There are so many ways to create AutoComplete textbox in ASP.NET using AJAX call but here, I'm using jQuery.
Before you start your project, download the jQuery file from - https://jquery.com/download/

After downloading, follow the below-mentioned few steps.

Step 1

Create one ASP.NET Project.

Step 2

Add Connection String in web config file in your project.
  1. <connectionStrings>
  2. <add name="conStr" connectionString="Database=Demo;data source=SQLEXPRESS; user id=sa;password=Abc123;" providerName="System.Data.SqlClient"/>
  3. </connectionStrings>
Step 3

Create the simple table named as Employee with EmpId, Name, and Address fields in SQL Server.
  1. CREATE TABLE [dbo].[Employee](
  2. [EmpId] [int] IDENTITY(1,1) Primary key NOT NULL ,
  3. [Name] [varchar](125) NULL,
  4. [Address] [varchar](125) NULL
  5. )
Step 4

Add Web Form AutoCompleteTextBox.aspx.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AutoCompleteTextBox.aspx.cs" Inherits="JQueryAutoCompleteTextBox.AutoCompleteTextBox" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Auto Complete Textbox</title>
  6. <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.8.0.js"></script>
  7. <script src="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.22/jquery-ui.js"></script>
  8. <link rel="Stylesheet" href="http://ajax.aspnetcdn.com/ajax/jquery.ui/1.8.10/themes/redmond/jquery-ui.css" />
  9. <script>
  10. $(document).ready(function () {
  11. $("#txtEmployee").autocomplete({
  12. source: function (request, response) {
  13. var param = { EmpName: $('#txtEmployee').val() };
  14. $.ajax({
  15. url: "AutoCompleteTextBox.aspx/getEmployees",
  16. data: JSON.stringify(param),
  17. dataType: "json",
  18. type: "POST",
  19. contentType: "application/json; charset=utf-8",
  20. dataFilter: function (data) { return data; },
  21. success: function (data) {
  22. console.log(JSON.stringify(data));
  23. response($.map(data.d, function (item) {
  24. return {
  25. value: item.EmpName +" ("+ item.Address+")"
  26. }
  27. }))
  28. },
  29. error: function (XMLHttpRequest, textStatus, errorThrown) {
  30. var err = eval("(" + XMLHttpRequest.responseText + ")");
  31. alert(err.Message)
  32. // console.log("Ajax Error!");
  33. }
  34. });
  35. },
  36. minLength: 1 //This is the Char length of inputTextBox
  37. });
  38. });
  39. </script>
  40. </head>
  41. <body>
  42. <form id="form1" runat="server">
  43. <div>
  44. <table>
  45. <tr>
  46. <td>
  47. <asp:Label ID="lblEmployee" Text="Employee Search" runat="server"></asp:Label>
  48. </td>
  49. <td>
  50. <asp:TextBox ID="txtEmployee" runat="server" Width="200" placeholder="Employee Name"></asp:TextBox>
  51. </td>
  52. </tr>
  53. </table>
  54. </div>
  55. </form>
  56. </body>
  57. </html>
Step 5

Write the code in the code behind file using [Webmethod], like this.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web.Services;
  4. using System.Data.SqlClient;
  5. using System.Configuration;
  6. namespace JQueryAutoCompleteTextBox
  7. {
  8. public partial class AutoCompleteTextBox : System.Web.UI.Page
  9. {
  10. protected void Page_Load(object sender, EventArgs e)
  11. {
  12. }
  13. [WebMethod]
  14. public static List<Employees> getEmployees(string EmpName)
  15. {
  16. List<Employees> empObj = new List<Employees>();
  17. string cs = ConfigurationManager.ConnectionStrings["conStr"].ToString();
  18. try
  19. {
  20. using (SqlConnection con=new SqlConnection(cs))
  21. {
  22. using (SqlCommand com = new SqlCommand())
  23. {
  24. com.CommandText = string.Format( "select EmpId,Name,Address from employee where name like '{0}%'", EmpName);
  25. com.Connection = con;
  26. con.Open();
  27. SqlDataReader sdr = com.ExecuteReader();
  28. Employees emp = null;
  29. while (sdr.Read())
  30. {
  31. emp = new Employees();
  32. emp.EmpDbKey = Convert.ToInt32(sdr["EmpId"]);
  33. emp.EmpName = Convert.ToString(sdr["Name"]);
  34. emp.Address = Convert.ToString(sdr["Address"]);
  35. empObj.Add(emp);
  36. }
  37. }
  38. }
  39. }
  40. catch (Exception ex)
  41. {
  42. Console.WriteLine("Error {0}",ex.Message);
  43. }
  44. return empObj;
  45. }
  46. }
  47. }
Summary

This article showed how to use a jQuery UI AutoComplete in ASP.NET using jQuery with a complex object. Let me know if you have a better approach for this. I'm waiting for your comments.