Click here to Skip to main content
15,896,310 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a combo box having different type of parameters and there is a textbox near to it and a find button is there .
suppose combobox having name,age,gender and if i will select name in combobox so in textbox i can only type some name and find that particular record from database by clicking find button next to it
so how can i do this in windows form in C# in visual studio 2010 ?
Posted

1 solution

Try:
C#
if (myComboBox.Text == "Name")
    {
    using (SqlConnection con = new SqlConnection(strConnect))
        {
        con.Open();
        using (SqlCommand cmd = new SqlCommand("SELECT Id, Description FROM myTable WHERE NameColumn LIKE '%' + @NAME + '%'", con))
            {
            cmd.Parameters.AddWithValue("@NAME", myTextBox.Text);
            using (SqlDataReader reader = cmd.ExecuteReader())
                {
                while (reader.Read())
                    {
                    int id = (int)reader["Id"];
                    string desc = (string)reader["Description"];
                    Console.WriteLine("ID: {0}\n    {1}", iD, desc);
                    }
                }
            }
        }
    }
 
Share this answer
 
Comments
BillWoodruff 18-Mar-14 0:54am    
+5 This is a great answer; why would anyone vote this #2 ?

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