In this article I will explain with an example, how to implement AutoComplete DropDownList using AJAX in ASP.Net with C# and VB.Net.
This article will illustrate use of ASP.Net AJAX AutoCompleteExtender populated from SQL Server Database in ASP.Net with C# and VB.Net.
 
 
Database
Here I am making use of Microsoft’s Northwind Database. You can download it from here.
 
 
Using the ASP.Net AJAX Control Toolkit
First you have to download and install the AJAX Control Toolkit DLL. For more details, refer my article Install AJAX Control Toolkit in Visual Studio ToolBox.
 
 
Registering ASP.Net AJAX Control Toolkit
In order to use ASP.Net AJAX Control Toolkit controls, you will need to add reference of AJAX Control Toolkit Library and then register on the Page as shown below.
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
 
 
HTML Markup
The HTML Markup consists of an ASP.Net ScriptManager control, ASP.Net TextBox and an ASP.Net AJAX AutoCompleteExtender control.
The AJAX Control Toolkit AutoCompleteExtender has been set with ServiceMethod property and the TargetControlID has been set with the Id of the TextBox control.
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
</asp:ScriptManager>
<asp:TextBox ID="txtContactsSearch" runat="server"></asp:TextBox>
<cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" ServiceMethod="SearchCustomers"
    MinimumPrefixLength="2" CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtContactsSearch" FirstRowSelected="false">
</cc1:AutoCompleteExtender>
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data.SqlClient;
using System.Configuration;
using System.Web.Services;
 
VB.Net
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.Services
 
 
Server Side Method
The following Web Method is used to populate the records from the Customers Table of the Northwind database.
This method accepts two parameters namely:
1. prefixText (string)
2. count (int)
Note: It is very important that you keep the signature of the method i.e. the name of the parameters are same as given, otherwise the method won’t work with AutoCompleteExtender.
 
The value of the ContactName field is inserted into a Generic List collection of string and then returned to the ASP.Net AJAX AutoCompleteExtender Control.
C#
[WebMethod]
public static List<string> SearchCustomers(string prefixText, int count)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManagerConnectionStrings["constr"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'";
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> customers = new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    customers.Add(sdr["ContactName"].ToString());
                }
            }
            conn.Close();
 
            return customers;
        }
    }
}
 
VB.Net
<WebMethod()>
Public Shared Function SearchCustomers(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
    Using conn As SqlConnection = New SqlConnection()
        conn.ConnectionString = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using cmd As SqlCommand = New SqlCommand()
            cmd.CommandText = "select ContactName from Customers where ContactName like @SearchText + '%'"
            cmd.Parameters.AddWithValue("@SearchText", prefixText)
            cmd.Connection = conn
            conn.Open()
            Dim customers As List(Of String) = New List(Of String)()
            Using sdr As SqlDataReader = cmd.ExecuteReader()
                While sdr.Read()
                    customers.Add(sdr("ContactName").ToString())
                End While
            End Using
            conn.Close()
 
            Return customers
        End Using
    End Using
End Function
 
 
Screenshot
AutoComplete DropDownList using AJAX in ASP.Net with C# and VB.Net
 
 
Downloads
Download Code