Home > DeveloperSection > Articles > Display Record According to ComboBox Selection in CSharp .NET

Display Record According to ComboBox Selection in CSharp .NET


ADO.Net ADO.Net 
Ratings:
1 Comment(s)
 8723  View(s)
Rate this:

Display Record According to ComboBox Selection


To display record according to ComboBox selection we have to search for the selected item value of combobox in database.

Example

This code is written in ComboBox  TextChanged event.

try

            {

SqlCommand cmd = new SqlCommand("select * from Employee where ID='" + cbID.Text.Trim() + "'", con);

//query for selecting only those record where ID is same as selected value of ComboBox

                SqlDataReader rdr = cmd.ExecuteReader();

//executing query and storing in DataReader.

                if (rdr.Read())

                {//reading records and if present then writing in textboxes

                    txtName.Text = rdr[1].ToString();

                    txtAddress.Text = rdr[2].ToString();                   

                }

                else

                {

                    txtName.Text = "";

                    txtAddress.Text = "";

                }

                cmd.Dispose();

                rdr.Dispose();

//disposing command and datareader so that we do not get error message

//when the command isexecuted again.

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

 


combobox slection

By John Smith on   5 years ago
nice article

Don't want to miss updates? Please click the below button!

Follow MindStick