Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
What I want is to implement the following Code
C#
private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            SendKeys.Send("{END}");
            comboBox2.Items.Clear();
            SqlConnection con = new SqlConnection();
            con.ConnectionString = Global.constr;
            con.Open();
            SqlCommand cmd = new SqlCommand("select itemCode from ItemDetails where  
                                            ItemCode like '" + comboBox2.Text + "%' 
                                            order by ItemCode", con);
            SqlDataReader dr = cmd.ExecuteReader();
            
            while (dr.Read())
            {
                comboBox2.Items.Add(dr[0].ToString());
            }
            dr.Close();
            con.Close();
            
        }


But the code is not working when I use the down arrow to choose an Item.
The comboBox2.Items.Clear(); code is clearing my Text Property also.
Searching is fine. But I can't choose the particular item from list. It is been cleared automatically.
Please help
Thanks in advance
Posted
Updated 26-Apr-17 5:20am
Comments
Sergey Alexandrovich Kryukov 26-Dec-11 19:03pm    
What do you want to achieve, exactly? What is "not working"? SendKeys is a dirty thing, not to be used to solve regular UI problems.
--SA
[no name] 27-Dec-11 5:22am    
I don't have any problem regarding SendKey.
My problem is ComboBox.Items.clear();

1 solution

Try following:

C#
private void comboBox2_TextChanged(object sender, EventArgs e)
        {
            SendKeys.Send("{END}");
            //comboBox2.Items.Clear(); NOT HERE
            SqlConnection con = new SqlConnection();
            con.ConnectionString = Global.constr;
            con.Open();
            SqlCommand cmd = new SqlCommand("select itemCode from ItemDetails where  
                                            ItemCode like '" + comboBox2.Text + "%' 
                                            order by ItemCode", con);
            SqlDataReader dr = cmd.ExecuteReader();
            comboBox2.Items.Clear(); // HERE
            while (dr.Read())
            {
                comboBox2.Items.Add(dr[0].ToString());
            }
            dr.Close();
            con.Close();
            
        }
 
Share this answer
 
Comments
[no name] 27-Dec-11 5:26am    
I already tried the above code. but still my text Property is being cleared by that code.
How to use Items in the text property.

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