What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleBind a ComboBox to a database lookup table in VB .NET
Keywordsdatabase, ADO.NET, VB .NET, bound controls, data binding, ComboBox, lookup table
CategoriesDatabase, VB.NET, Software Engineering, Controls
This example uses a database with two tables:

UserTypes: UserTypeId UserTypeName

The value in Users.UserType is a key into the lookup table UserTypes. The constraint is Users.UserType = UserTypes.UserTypeId.

When the program is displaying data for a Users record at run time, the ComboBox should display the corresponding UserTypes.UserTypeName value.

First, some geneal instructions for creating database connections, data adapters, and DataSets.

  • Database Connection:
    1. Open the form designer. On the Toolbox, click the Data tab. Double click the OldDbConnection tool.
    2. Select the new connection object. In the Properties window, click on the ConnectionString property. Click the dropdown to the right and select . Use the resulting Data Link Properties dialog to specify the data provider and database.

  • Data Adapter:
    1. Open the form designer. On the Toolbox, click the Data tab. Double click the OldDbDataAdapter tool.
    2. Work through Data Adapter Configuration Wizard.
      1. Select the database connection you just created.
      2. Select Use SQL Statements.
      3. Enter a SQL statement to select the records you want to display (or use the Query Builder to build a SQL statement).
      4. Click Finish.

  • DataSet:
    1. Open the form designer. Open the Data menu and select Generate DataSet. Select the New option, enter a descriptive name for the generic DataSet type (e.g. DataSetUsers), select the table(s) you want to use, leave the "Add this dataset to the designer" box checked, and click OK.
    2. Initially the new DataSet is named something like DataSetUsers1. Change the name to something more consistent with your naming conventions such as dsUsers.

Now for the specific instructions.

  1. Build a database connection (connUsers) as usual.
  2. Build two data adapters, one for the Users table (daUsers) and one for the UserTypes table (daUserTypes).
  3. Make the DataSet (dsUserData), selecting all fields in both tables.
  4. Make a many-to-one relationship in the DataSet:
    1. Right-click the DataSet and select View Schema.
    2. Click the Schema button on the lower left.
    3. Drag the UserTypes.UserTypeId field onto the Users.UserType field. In the resulting dialog, fill in the Users.UserType field.
    4. Close the schema, accepting the changes.

  5. Set the ComboBox's properties.
    1. Set DataSource = the lookup table: dsUserData.UserTypes.
    2. Set DisplayMember = the field to display in the lookup table: UserTypeName.
    3. Set ValueMember = the field that is the actual value in the lookup table: UserTypeId.
    4. Bind the SelectedValue property:
      1. Open the ComboBox's DataBindings property (at the top in the Properties window).
      2. Select the SelectedValue property. Click the dropdown arrow on the right.
      3. Select the field in the main Users record that you want the ComboBox to represent: dsUserData.Users.UserType.
  6. Bind other controls if desired.

At run time, you still need to load and save the database data. In the form's Load event handler, call the data adapters' Fill methods to fill the DataSet. In this case, the program must load the UserTypes data first so it will be available when the Users table is loaded (the Users.UserType value must match a UserTypes.UserTypeId value).

' Load the data.
In the form's Closing event handler, the program saves changes to the Users table.
' Save any changes to the data.
For more information on using databases with VB .NET, see my book Visual Basic .NET Database Programming.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.