In this tutorial, I am going to show you how to get data from a database table and fill the combo box.
Step 1:
Add required namespace in your project i.e. using System.Data.SqlClient;
Step 2:
Drag and drop a combo box control into your form and assign any name to it. For example, I have assigned here: “personComboBox”.
Step 3:
Write the below-mentioned code in your event handler.
SqlConnection con = new SqlConnection ("Type your connection string here");
try
{
con.Open();
}
catch (SqlException ex)
{
throw ex;
}
SqlDataAdapter da =
new SqlDataAdapter ("select person_name from person", con);
DataSet dt=new DataSet();
da.Fill(dt);
personComboBox.DataSource =dt.Tables[0];
personComboBox.DisplayMember = "person_name";
con.Close();
No comments:
Post a Comment
Please do not enter spam links.