Click here to Skip to main content
15,921,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a textbox1 and it's autocomplete source is on database .. but it's not working as it should .. as an example :

I have on my table these names :

united states
united kingdom


and the user start typing "kingdom" no suggestion appear only when I type it from the beginning "united kingdom"

I need the suggestion appear if I type starting from the middle or end or beginning

What I have tried:

C#
cm = new SqlCommand();
cm.Connection = cn;
string searchFor = "%" + textBox1.Text + "%"; //the string the user entered.
AutoCompleteStringCollection namecollection = new AutoCompleteStringCollection();
cm.CommandText = @"SELECT distinct itmname AS name FROM table1 WHERE name LIKE @name";

cm.Parameters.AddWithValue("@name", searchFor);
SqlDataReader rea = cm.ExecuteReader();
if (rea.HasRows == true)
{
    while (rea.Read())
        namecollection.Add(rea["name"].ToString());
}
rea.Close();
autoCompleteTextbox1.AutoCompleteCustomSource = namecollection;
Posted
Updated 28-Feb-16 18:43pm
v2
Comments
CHill60 28-Feb-16 15:31pm    
That's the way autocomplete works. If you want some other behaviour you'll have to write to code for it
medo0- 28-Feb-16 15:37pm    
that's why i'm asking .. thanks
CHill60 28-Feb-16 15:39pm    
Have you tried google - there are several suggested methods out there

1 solution

Hi
you can take the entire data to datatable and use a linq query to get results which contains the text you entered in textbox


refer this link

AutoComplete TextBox with SubString search, similar to SQL Like or Contains[^]

Thanks
 
Share this answer
 
v2
Comments
medo0- 14-Mar-16 18:23pm    
thanks for your reply

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