How to obtain the data from the database and then bind the data to controls by using Visual Basic .NET or Visual Basic 2005 (821763)
The information in this article applies to:
- Microsoft Visual Basic 2005
- Microsoft Visual Basic .NET (2003)
- Microsoft Visual Basic .NET (2002)
SUMMARYThis article describes how to obtain the data from the
database and then bind the data to controls. You can use the DataSet class and the DataAdapter class to obtain the data from the database. You can use the DataBindings property of the control to bind the data of the DataSet object to the control. The step-by-step example in this
article describes how to connect to the Northwind sample database
(Northwind.mdb) in Microsoft Access and then obtain data by using the OleDbDataAdapter class. In the example, you add the Binding objects to the ControlBindingsCollection class to bind the data that you obtain to the TextBox control and to the ListBox control on the Microsoft Windows form. back to the topRequirementsThe following list outlines the recommended hardware, software,
network infrastructure, and service packs that are required:
- Microsoft Visual Studio .NET or Microsoft Visual Studio 2005
- Northwind.mdb in Access
This article assumes that you are familiar with the following
topics:
- Microsoft Visual Basic .NET or Microsoft Visual Basic 2005 syntax
- Windows forms controls
- Connectivity to the Access database
back to the top Create a Microsoft Windows ApplicationTo create a new Windows application by using Visual Basic .NET or Visual Basic 2005,
follow these steps:
- Start Visual Studio .NET or Visual Studio 2005.
- On the File menu, point to
New, and then click Project.
- Under Project Types, click Visual
Basic Projects.
Note In Visual Studio 2005, click Visual Basic under Project Types. - Under Templates, click Windows
Application, and then click OK.
By default,
Form1 is created. back to the topConnect to the Northwind Database to Obtain the DataConnect to Northwind.mdb in Access by using the Microsoft OLE
DB provider. Use the OleDbDataAdapter class to obtain data from the database. The Fill method of the OleDbDataAdapter class loads the data into the DataSet, as follows:
- On the View menu, click Server
Explorer.
- In Server Explorer, right-click Data
Connections, and then click Add
Connection.
- In the Data Link Properties dialog box,
click the Provider tab.
- Click Microsoft Jet 4.0 OLE DB Provider on
the OLE DB Provider(s) list, and
then click Next.
- On the Connection tab, click the ellipses
(...) button.
- In the Select Access Database dialog box,
click the Program Files folder, click the Microsoft Office
folder, click the Office10 folder, and then click the Samples file.
- Click the Northwind.mdb file, and then
click Open.
Note If you use Microsoft Office Access 2002, the Samples folder is located in the Office10 folder. If you use Microsoft Office Access 2003, the Samples folder is located in the Office11 folder. - To verify that you are
connected to Northwind.mdb, click Test Connection.
If the test connection succeeds, click OK in the Data
Link Properties dialog box. - In Server Explorer, expand your Access database connection,
and then expand Tables.
- Drag the Employees table to
Form1.
By default, OleDbConnection1 and
OleDbDataAdapter1 are created. - Right-click OleDbDataAdapter1, and then
click Generate Dataset.
- In the Generate Dataset dialog box, click
OK.
- Right-click DataSet11, and then click
Properties.
- In the Properties dialog box, set the
Name property to ds.
If you are using Visual Studio 2005, use the following steps: - On the View menu, point to Other Windows, and then click Server
Explorer.
- In Server Explorer, right-click Data
Connections, and then click Add
Connection.
- In the Choose Data Source dialog box,
click Microsoft Access Database File under Data source, and then click Continue.
- Click the Brown button.
- In the Select Microsoft Access Database File dialog box,
click the Program Files folder, click the Microsoft Office
folder, click the OFFICE11 folder, and then click the SAMPLES file.
- Click the Northwind.mdb file, and then
click Open.
- To verify that you are
connected to Northwind.mdb, click Test Connection. If the test connection succeeds, click OK in the Add Connection dialog box.
- In Server Explorer, expand your Access database connection,
and then expand Tables.
- Drag the Employees table to
Form1.
By default, OleDbConnection1 and
OleDbDataAdapter1 are created. - Right-click OleDbDataAdapter1, and then
click Generate Dataset.
- In the Generate Dataset dialog box, click
OK.
- Right-click DataSet11, and then click
Properties.
- In the Properties dialog box, set the
Name property to ds.
back to the topBind the Data to the ControlsTo bind the data to the controls, add the Binding object to ControlBindingsCollection. The Binding object creates and then maintains a simple binding between the
property value of an object and the property value of the control. You can use
the DataBindings property to access ControlBindingsCollection. You can create Binding objects as follows:
- Specify the property of the control that you want to bind
the data to. For example, to display data in a TextBox
control, specify the Text property.
- Specify the instance of a data source. For example, use the
DataSet as a data source.
- Specify the navigation path. The navigation path may be an
empty string (""), may be a single property name, or may be a
period-delimited hierarchy of names. When you set the navigation path to an
empty string, the ToString method is called on the underlying
data source object. For example, if you use a DataSet as a
data source, you can specify TableName.ColumnName as the
navigation path.
The BindingManagerBase class enables you to synchronize data-bound controls on a Windows
form that are bound to the same data source. In the following example, the
Windows form contains a ListBox control, TextBox controls, and RichTextBox controls that are bound to the same data source but are bound to different
columns of the Employee data table. You must synchronize the controls to display
the details of the same employee. The CurrencyManager class inherits from the BindingManagerBase class. CurrencyManager synchronizes the controls by maintaining a pointer to the current item. The controls
are bound to the current item. Therefore, they display the information for the
same row. To bind the data in the DataSet to controls on Form1, follow these steps:
- Add two Panel controls to
Form1.
- Add two Label controls to
Form1.
- Put Label1 above Panel1,
and then put Label2 above Panel2.
- In the Properties dialog box, set the
Text property of Label1 to Select the
Employee, and then set the Text property of
Label2 to Details of the Employee.
- Add a ListBox control to Form1.
- Put ListBox1 in
Panel1.
- In the Properties dialog box of
ListBox1, set the DataSource property to
ds, and then set the DisplayMember property
to Employees.FirstName.
- Add five TextBox controls to Form1. Put
the controls in Panel2.
- In the Properties dialog box, set the
Name property of the TextBox control and set
the Text property of the TextBox control to
the following values:
Control | Name | Text | TextBox1 | EmpId | EmpId | TextBox2 | LastName | LastName | TextBox3 | BirthDate | BirthDate | TextBox4 | Title | Title | TextBox5 | HomePhone | HomePhone |
- Add two RichTextBox controls to Form1. Put
the controls in Panel2.
- In the Properties dialog box, set the
Name property of the RichTextBox control and
set the Text property of the RichTextBox
control to the following values:
Control | Text | Name | RichTextBox1 | Notes | Notes | RichTextBox2 | Address | Address |
- Add seven Label controls to Form1. Put
each Label control on the left side of the
TextBox control and on the left side of the
RichTextBox control as follows:
Control Name | Label | EmpId | Label3 | LastName | Label4 | BirthDate | Label5 | Title | Label6 | HomePhone | Label7 | Notes | Label8 | Address | Label9 |
- In the Properties dialog box, set the
Text property of the Label controls to the
following values:
Control Name | Text Value | Label3 | Employee
ID | Label4 | Last
Name | Label5 | Date of
Birth | Label6 | Title | Label7 | Home Phone
Number | Label8 | Notes | Label9 | Address of the
Employee |
- Add the following code to the Form1_Load
event handler:
'Adds rows to dataset.
OleDbDataAdapter1.Fill(ds)
'Add a binding object to the controls.
'Set Text as the data-bound property, and set ds as the data source.
'TableName.ColumnName specifies the data member.
BirthDate.DataBindings.Add(New Binding("Text", ds, "Employees.BirthDate"))
LastName.DataBindings.Add(New Binding("Text", ds, "Employees.LastName"))
Notes.DataBindings.Add(New Binding("Text", ds, "Employees.Notes"))
Title.DataBindings.Add(New Binding("Text", ds, "Employees.Title"))
HomePhone.DataBindings.Add(New Binding("Text", ds, "Employees.HomePhone"))
Address.DataBindings.Add(New Binding("Text", ds, "Employees.Address"))
EmpId.DataBindings.Add(New Binding("Text", ds, "Employees.EmployeeID")) back to the topVerify That It Works- On the Build menu, click Build
Solution.
- On the Debug menu, click
Start.
- In the
ListBox control, click an employee name.
The
employee details are available in the TextBox controls and in
the RichTextBox controls. back to the
topREFERENCES For additional
information, click the following article number to view the article in the
Microsoft Knowledge Base: 313482
INFO: Roadmap for Windows Forms Data Binding
For more information about the Control.DataBindings
property, visit the following
Microsoft Developers Network (MSDN) Web site: back to the
top
Modification Type: | Minor | Last Reviewed: | 10/3/2006 |
---|
Keywords: | kbvs2005applies kbvs2005swept kbWindowsForms kbForms kbDataBinding kbCtrl kbControl kbHOWTOmaster KB821763 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|