EDN Admin
Well-known member
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?
<font size=2>
</font><font color="#008000" size=2>//Connect to the Sales database </font><font size=2>
</font><font color="#008000" size=2>//Read in all of the Customers from the Customer table </font><font size=2>
</font><font color="#008000" size=2>// into a Dataset (an in-memory relational </font><font size=2>
</font><font color="#008000" size=2>//database. </font><font size=2>
</font><font color="#008000" size=2>//Run through this dataset, adding their names to the combobox. </font><font size=2>
</font><font color="#008000" size=2>//We can blow away the dataset at that point. </font><font size=2>
</font><font color="#008000" size=2>//declare these outside the try so we can close them off in the finally clause. </font><font size=2>
SqlConnection oConnection = </font><font color="#0000ff" size=2>null</font><font size=2>;
DataSet oCustomersDataSet = </font><font color="#0000ff" size=2>new</font><font size=2> DataSet();
</font><font color="#0000ff" size=2>try </font><font size=2>
{
</font><font color="#008000" size=2>//ensure the box is cleared </font><font size=2>
cboCombo.Items.Clear();
</font><font color="#008000" size=2>//set up the connection </font><font size=2>
oConnection = </font><font color="#0000ff" size=2>new</font><font size=2> SqlConnection("Server=KOBNA\SQLEXPRESS;Database=shefa;User ID=sa;Password=111111;Trusted_Connection=False");
</font><font color="#008000" size=2>//set up the data adapter to get us our data... </font><font size=2>
</font><font color="#008000" size=2>//Adapters xfer data to/from Database and Dataset. </font><font size=2>
SqlDataAdapter oDataAdapter = </font><font color="#0000ff" size=2>new</font><font size=2> SqlDataAdapter();
</font><font color="#0000ff" size=2>string</font><font size=2> sCommand = "SELECT * FROM marital_status";
oDataAdapter.SelectCommand = </font><font color="#0000ff" size=2>new</font><font size=2>
SqlCommand(sCommand,oConnection);
</font><font color="#008000" size=2>//use the data adapter to fill the dataset with the result </font><font size=2>
</font><font color="#008000" size=2>// of that SQL statement </font><font size=2>
</font><font color="#008000" size=2>//Note we have changed the table name so that in memory we run </font><font size=2>
</font><font color="#008000" size=2>// off the BoxCustomers </font><font size=2>
</font><font color="#008000" size=2>//table...just to make the point that we are working off a </font><font size=2>
</font><font color="#008000" size=2>// construct of our own devising. </font><font size=2>
oDataAdapter.Fill(oCustomersDataSet, "marital_status");
</font><font color="#008000" size=2>//can now close the database connection, well work off the dataset </font><font size=2>
</font><font color="#008000" size=2>// which is in memory. </font><font size=2>
oConnection.Close();
</font><font color="#008000" size=2>//we are now disconnected. </font><font size=2>
</font><font color="#008000" size=2>//put the data from the dataset into the combobox </font><font size=2>
DataTable oDataTable = oCustomersDataSet.Tables["marital_status"];
</font><font color="#0000ff" size=2>if</font><font size=2> (oDataTable == </font><font color="#0000ff" size=2>null</font><font size=2>)
</font><font color="#0000ff" size=2>throw</font><font size=2> </font><font color="#0000ff" size=2>new</font><font size=2> Exception("BoxCustomers table not found in oCustomersDataSet.");
cboCombo.DataSource = oDataTable;
cboCombo.DisplayMember = "marital_status";
</font><font color="#008000" size=2>//the finally clause will tidy up for us. </font><font size=2>
cboCombo.SelectedIndex = 0;
}
</font><font color="#0000ff" size=2>catch</font><font size=2> (Exception oE)
{
MessageBox.Show("Problem Populating Dataset Box: [" + oE.ToString() + "]");
}
</font><font color="#0000ff" size=2>finally </font><font size=2>
{
</font><font color="#008000" size=2>//clear the inmemory data in the dataset, and close the database </font><font size=2>
</font><font color="#008000" size=2>// connection, if its open. </font><font size=2>
</font><font color="#0000ff" size=2>if</font><font size=2> (oConnection!= </font><font color="#0000ff" size=2>null</font><font size=2>)
{
</font><font color="#0000ff" size=2>if</font><font size=2> (oConnection.State == ConnectionState.Open) oConnection.Close();
}
} </font>
View the full article