Click here to Skip to main content
15,889,462 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I need to autocomplete textbox value with based on middle value from database. but it working autocomplete when type first letter not working middle of the letter .

What I have tried:

textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
                textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                AutoCompleteStringCollection DataCollection2 = new AutoCompleteStringCollection();
                getData2(DataCollection2);
                textBox1.AutoCompleteCustomSource = DataCollection2;



private void getData2(AutoCompleteStringCollection dataCollection)
        {
            try
            {
                string customer= "Customer";
                string connetionString = null;
                SqlConnection connection;
                SqlCommand command;
                SqlDataAdapter adapter = new SqlDataAdapter();
                DataSet ds = new DataSet();
                connetionString = System.Configuration.ConfigurationSettings.AppSettings["connection"];
                string sql = "SELECT [party_name] FROM [Party_details] where party_name like '%" +textBox1.Text+ "%'";
               
                connection = new SqlConnection(connetionString);
                try
                {
                    connection.Open();
                    command = new SqlCommand(sql, connection);
                    adapter.SelectCommand = command;
                    adapter.Fill(ds);
                    adapter.Dispose();
                    command.Dispose();
                    connection.Close();
                    foreach (DataRow row in ds.Tables[0].Rows)
                    {
                        dataCollection.Add(row[0].ToString());
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Can not open connection ! ");
                }
            }
            catch (Exception trt)
            { }
        }
Posted
Updated 23-Oct-17 3:56am
Comments

1 solution

The below article has code example, I hope this will help you. The drawback is, you will be fetching entire data from database to datatable and then applying the filter.

Autocomplete combobox match any part of string not only beginning string.
 
Share this answer
 

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