I am using the following coee to bind a combo box on a form and i want to bind more boxes (I have 7 combos on one form).
each combo reads from a different table but in the same database.
how can i modify the code to add combo2 wihout slowing the performance of form loading
//Connect to the Sales database //Read in all of the Customers from the Customer table // into a Dataset (an in-memory relational //database. //Run through this dataset, adding their names to the combobox. //We can blow away the dataset at that point. //declare these outside the try so we can close them off in the finally clause.
SqlConnection oConnection =
null;DataSet oCustomersDataSet =
new DataSet(); try{
//ensure the box is clearedcboCombo.Items.Clear();
//set up the connectionoConnection =
new SqlConnection("Server=KOBNA\\SQLEXPRESS;Database=shefa;User ID=sa;Password=111111;Trusted_Connection=False"); //set up the data adapter to get us our data... //Adapters xfer data to/from Database and Dataset.SqlDataAdapter oDataAdapter =
new SqlDataAdapter(); string sCommand = "SELECT * FROM marital_status";oDataAdapter.SelectCommand =
newSqlCommand(sCommand,oConnection);
//use the data adapter to fill the dataset with the result // of that SQL statement //Note we have 'changed' the table name so that in memory we run // off the BoxCustomers //'table'...just to make the point that we are working off a // construct of our own devising.oDataAdapter.Fill(oCustomersDataSet, "marital_status");
//can now close the database connection, we'll work off the dataset // which is in memory.oConnection.Close();
//we are now disconnected. //put the data from the dataset into the comboboxDataTable oDataTable = oCustomersDataSet.Tables["marital_status"];
if (oDataTable == null) throw new Exception("BoxCustomers table not found in oCustomersDataSet.");cboCombo.DataSource = oDataTable;
cboCombo.DisplayMember = "marital_status";
//the finally clause will tidy up for us.cboCombo.SelectedIndex = 0;
}
catch (Exception oE){
MessageBox.Show("Problem Populating Dataset Box: [" + oE.ToString() + "]");
}
finally{
//clear the inmemory data in the dataset, and close the database // connection, if it's open. if (oConnection!= null){
if (oConnection.State == ConnectionState.Open) oConnection.Close();}
}

data binding multiple combo boxes
Raminator74
Hi!
You can this way - read DB in background thread (so UI stays alive), 7 calls to 7 data adapters. Here is concept sample:
protected override void OnLoad(object sender, EventArgs e)
{
}