Click here to Skip to main content
15,899,679 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i want to use auto complete textbox using ajax with the multiple tables and the list of the autocomplete with the table name and search items.

What I have tried:

html code :

HTML
<asp:ScriptManager ID="ScriptManager1" runat="server"
EnablePageMethods = "true">

 
<asp:TextBox ID="txtContactsSearch" runat="server">
<cc1:AutoCompleteExtender ServiceMethod="SearchCustomers"
    MinimumPrefixLength="2"
    CompletionInterval="100" EnableCaching="false" CompletionSetCount="10"
    TargetControlID="txtContactsSearch"
    ID="AutoCompleteExtender1"  runat="server" FirstRowSelected = "false">


C# code :
C#
public static List<string> SearchCustomers(string prefixText, int count)
{
    using (SqlConnection conn = new SqlConnection())
    {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["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;
        }
    }
} 
Posted
Updated 27-Jul-16 4:06am
v4
Comments
ZurdoDev 27-Jul-16 9:58am    
If you want to search two tables then change your sql to do so. Where are you stuck?

1 solution

 
Share this answer
 
Comments
Ravinder Kumar 27-Jul-16 9:50am    
Thanks for your suggestion sir, But i want to search on multiple tables. it is working on only one table
deepankarbhatnagar 27-Jul-16 10:20am    
So create query with join and get all tables data in a list from where you can get search accordingly.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900