|
HTML clipboard
using System.Data.SqlClient;
Using a DataReader to Populate a Combo-box
Imagine we have a combo-box cboReaderBox, and a button called PopulateReaderBox
that we want to use to populate that box. The code for the button Click handler
would be as follows:
private void btnPopulateReaderBox_Click(object
sender, System.EventArgs e)
{
SqlDataReader oReader = null;
SqlConnection oConnection = null;
try
{
cboReaderBox.Items.Clear();
oConnection = new
SqlConnection("server=localhost;uid=sa;pwd=;database=Sales");
string
sSelectSQL = "SELECT FirstName +
' ' + LastName as Name FROM Customer";
SqlCommand oCommand =
new SqlCommand(sSelectSQL,oConnection);
oConnection.Open();
oReader = oCommand.ExecuteReader();
if (oReader
!= null)
{
while (oReader.Read())
{
cboReaderBox.Items.Add(oReader["Name"]);
}
}
cboReaderBox.SelectedIndex = 0;
}
catch (Exception oE)
{
MessageBox.Show("Problem Populating
Reader Box:
[" + oE.ToString() + "]");
}
finally
{
if (oReader!=null)
oReader.Close();
if (oConnection!=
null)
{
if (oConnection.State == ConnectionState.Open)
oConnection.Close();
}
}
}
This code does the following things:
1. Sets up a connection to the database
2. Sets up the command (a SELECT) ready to run
3. Opens a DataReader (like a forward-only record set) on that connection and
command
4. Loops through the rows that get returned by the reader, adding the ‘Name’
column to the combo-box’s Item collection.
5. There is a try-catch-finally structure here just to add robustness, you don’t
necessarily need this to get the code to work, but I like error handling.
Using a DataSet to Populate a Combo-box
Here is the code to populate another combo-box (cboDatasetBox) with the same
data, but this time from an in-memory copy the database or DataSet.
private void btnPopulateDatasetBox_Click(object sender, System.EventArgs e)
{
new DataSet();
try
{
new
new string sCommand = "SELECT FirstName + ' ' +
LastName as Name FROM Customer";
oDataAdapter.SelectCommand = new
if (oDataTable == null)
throw new Exception("BoxCustomers table not
found in oCustomersDataSet.");
foreach (DataRow oRow in oDataTable.Rows)
{
cboDatasetBox.Items.Add(oRow["Name"]);
}
catch (Exception oE)
{
MessageBox.Show("Problem Populating Dataset Box:
[" + oE.ToString() + "]");
}
finally
{
if
(oCustomersDataSet != null) oCustomersDataSet.Clear();
if (oConnection!= null)
{
if (oConnection.State == ConnectionState.Open)
oConnection.Close();
}
}
}
SqlConnection oConnection = null;
DataSet oCustomersDataSet =
cboDatasetBox.Items.Clear();
oConnection =
SqlConnection("server=localhost;uid=sa;pwd=;database=Sales");
SqlDataAdapter oDataAdapter =
SqlDataAdapter();
SqlCommand(sCommand,oConnection);
oDataAdapter.Fill(oCustomersDataSet,"BoxCustomers");
oConnection.Close();
DataTable oDataTable = oCustomersDataSet.Tables["BoxCustomers"];
cboDatasetBox.SelectedIndex = 0;
}
There are a few differences from the DataReader example:
1. It uses a DataAdapter. DataAdapters are the objects that handle communication
between the database itself and your in-memory DataSet. We use this DataAdapter
to ‘Fill()’ the DataSet with the results of our SELECT statement. The DataSet
will then contain an in-memory version of the database Customer table.
2. Just to make the point that we don’t need the database anymore, the second
parameter of the Fill() method specifies what to call the new in-memory table. I
decided to call it ‘BoxCustomers’. In other respects it is identical to the
actual Customers table in the database.
3. Notice we Close() the Connection to the database once the Fill() is done.
With the data in memory, we no longer need the database.
4. DataSets can contain whole databases, so to populate the combo-box we
explicitly specify the DataTable object pertaining to our new BoxCustomers
in-memory table. We use this DataTable object to populate the combo-box.
Binding a Combo-box to a DataTable
With the table loaded into memory, we have a third option, which is to Bind the
combo-box directly to that DataTable. The code for this is identical to the
second example above, except:
1. Instead of foreach-ing through the DataRows in the DataTable, we simply:
cboBoundSetBox.DataSource = oDataTable;
cboBoundSetBox.DisplayMember = "Name";
2. Add the end (in the finally clause), we don’t Clear() the DataSet, as we
would lose the data in the Table we have bound to the combo-box, thus emptying
the combo-box again (except for any selected item)!
|