To read the article online, visit

Teach Yourself ADO.NET in 24 Hours
Hour 9: Binding Data to List Controls

Simple Data Binding in Windows Forms

Let's quickly step through an example that will display a Customer ID, Company Name, and Contact Name from the Customer table in the Northwind database supplied in Microsoft SQL Server. More specifically, you will be creating a fewTextBox controls on a form and binding theText property on theseTextBox controls to the Customers data source. Then, you will create a button that will activate the data retrieval and display the data to the form. This is simple binding.

Create a New Project in VS .NET

  1. Create a new project in VS .NET by choosing File, New, and then choosing the Project option.

  2. When the New Project dialog box appears, choose Visual Basic Projects (or Visual C# Projects) and Windows Applications. Name this project "ADO.NET24hoursDB." This creates a default form for you to start from.

Add the Data Connection, Data Adapter, andDataSet

We will be accessing the Customers table in SQL Server's Northwind database. So, first we will need to create a data connection and a data adapter to Microsoft SQL Server.

  1. From the Data tab of the Toolbox, drag aSQLDataAdapter object into your form as shown inFigure 9.1 (orOleDBDataAdapter if you want).

    This will automatically invoke the Data Adapter Configuration Wizard. Both the data connection and the data adapter can be fully configured here.

    1. The wizard starts with the Choose Your Data Connection dialog box. If you already have a connection defined in your project, it will be placed in the dialog box. Otherwise, choose to create a new connection and specify the appropriate connection information (test the connection as well).

    2. You will then have to decide to supply SQL statements, build a new stored procedure, or give the name of an existing stored procedure for the data access. In our example we will use the Use SQL Statements option.

    3. You will be presented with a Generate the SQL Statements dialog box where you will simply type in a valid SQL statement, or you can use the Query Builder option to formulate the SQL query. For our example, just type in the following query:

      SELECT * FROM Customers
    4. The wizard will show you the tasks that it has done and indicate whether the SqlDataAdapter has been configured successfully.

Figure 9.1
Visual Studio .NET Form with Data ToolboxSqlDataAdapterobject selected.

  1. After theSqlDataAdapter andDataConnection objects have been configured and added to the form, you must generate aDataSet and then add an instance of thisDataSet to the form. We will be binding ourTextBox properties to the columns in theDataSet.

    1. Simply right-click on theSqlDataAdapter (SqlDataAdapter1) that is on your form and choose the Generate Dataset menu option as seen inFigure 9.2.

    2. Now, just choose to create anewDataSet using the default name that it provides (DataSet1). Make sure you have checked the Customers table and checked the box for it to be added to the designer.

    3. When the process finishes, aDataSet instance namedDataSet11 will be on the form and a dataset schema will be in the Solutions Explorer (named DataSet1.xsd).

Figure 9.2
Generate a new dataset for the form.

Create Text Boxes, Labels, and Buttons

The next step is to complete the form example to include a few text boxes and a control button. From the Windows Forms tab of the Toolbox, add the following (drag and drop on the form):

  • Textbox—With aname oftxtCustomerID andtext is blank.

  • Textbox—With aname oftxtCompanyName andtext is blank.

  • Textbox—With aname oftxtContactName andtext is blank.

  • Button—With aname ofbtnGetCustomer andtext of "Get Customer".

Go ahead and add labels in front of each text box so that it looks like the form inFigure 9.3.

Figure 9.3
Add text boxes, button, and labels to the form.

Add Code to Populate theDataSet

Now we are ready to complete the application by adding the code to fill theDataSet.

Just double-click on the Get Customer button to create a method for theClick event. You will have to add code to make a call to theDataSet'sClear method to clear theDataSet out between iterations, and make a call to the data adapter'sFill method to get data from the database (as you can see inFigure 9.4). The following code is added:


Figure 9.4
Adding code for theButton method.

Bind the Text Boxes to theDataSet

Nothing is left to do other than bind (simple binding) the text boxes to the columns in theDataSet and run the application.

  1. From the Forms Designer, select thetxtCustomerID text box and press F4. This will position you to the properties window for this text box.

  2. Expand the(DataBindings) node in the properties list and itstext property.

  3. Within the text property, expandDataSet1 and Customers nodes and select the CustomerID column from the list (look back atFigure 9.3 in the lower right corner to see the(DataBinding) property).

  4. Now, from the Forms Designer, select thetxtCompanyName text box and press F4.

  5. Expand the(DataBindings) node in the properties list and itstext property.

  6. Within thetext property, expand theDataSet1 andCustomers nodes and select the CompanyName column from the list.

  7. Finally, from the Forms Designer, select thetxtContactName text box and press F4.

  8. Expand the(DataBindings) node in the properties list and itstext property.

  9. Within thetext property, expand theDataSet1 andCustomers nodes and select the ContactName column from the list.

Test It!

That's it! Now just hit the F5 key and test your application by clicking on the Get Customer button. It will put the first customer's information (CustomerID, CompanyName, and ContactName) that it finds in theDataSet into the appropriate text boxes. InFigure 9.5, you can see the form displaying a customer's information successfully.

Figure 9.5
The Get Customer Forms application, showing simple binding.

The following snippet of code is from the forms1.vb program for this example and shows the explicit data binding for each text box. When you are using the Forms Designer, much of this code is generated for you (as you specify the data binding properties):

Me.txtCustomerID.DataBindings.Add(New System.Windows.Forms.Binding
("Text", Me.DataSet11, "Customers.CustomerID"))
Me.txtCompanyName.DataBindings.Add(New System.Windows.Forms.Binding
("Text", Me.DataSet11, "Customers.CompanyName"))
Me.txtContactName.DataBindings.Add(New System.Windows.Forms.Binding
("Text", Me.DataSet11, "Customers.ContactName"))

List Controls andDataGrid Controls - These are the key properties for list controls andDataGrid controls:

  • DataSource—An object thatmust implement theIlist interface such as aDataSet or an array

  • IList inherits from

  • Icollection—A set of objects of similar type

  • Ienumerable—Provides an enumerator, which allows you to traverse a collection one item at a time (via theMoveNext() method).

  • DisplayMember/DataMember—allows you to specify a table or elements within a table.

Note: The classes and interfaces in the System.Web.UI.WebControls namespace are similar and equivalent.

  • Read Part 3

  • Article Information
    Article Title: Article (Title Unknown)
    Article Author: Author Unknown
    Article URL:

    Copyright 2018 QuinStreet Inc. All Rights Reserved.
    Legal Notices, Licensing, Permissions, Privacy Policy.
    Advertise | Newsletters | E-mail Offers