data binding multiple combo boxes

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 cleared

cboCombo.Items.Clear();

//set up the connection

oConnection = 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 = new

SqlCommand(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 combobox

DataTable 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();

}

}




Answer this question

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)

    {

    DataSet dataSet = new DataSet();

    string errorMessage = null;

    new MethodInvoker(delegate

    {

    // Now we are inside background worker thread, do not call UI here!

    try

    {

    using(SqlConnection con = new SqlConnection(ConString))

    {

    con.Open();

    adapter1.Fill(dataSet);

    adapter2.Fill(dataSet);

    adapter3.Fill(dataSet);

    }

    }

    catch(Exception ex)

    {

    ErrorMessage = ex.Message;

    }

    // Call UI to show data on screen

    this.BeginInvoke(new MethodInvoker(delegate
    {

    // Now we again in UI thread

    if(string.IsNullOrEmpty(ErrorMessage))

    {

    Combo1.DataSource = dataSet;

    // ...and other binding code

    }

    else

    {

    MessageBox.Show(ErrorMessage);

    }

    }));

    }).BeginInvoke(null, null);

    }



  • data binding multiple combo boxes