Implementing Simple Data Binding in visual basic .Net
Navigating between Records
For every data source that is bound to a Windows Form control, there exists a CurrencyManager object. The CurrencyManager object handles the binding to the data source by keeping a pointer to the current item in the record list.
The CurrencyManager class is derived from the BindingManagerBase class. If all the Windows Form controls are bound to a single data source, the form will have one CurrencyManager object associated with it.
A BindingContext object, which is a Windows Form object, is used to keep track of the existing CurrencyManager objects in a form.
A Windows Form containing bound controls has at least one BindingContext object that keeps track of one or more CurrencyManager objects, each in turn keeping track of a data source. The figure given below illustrates the relationship between the BindingContext class, CurrencyManager class, and a Windows Form:
Fig:- 1.1. Relationship between the BindingContext class, CurrencyManager class and a Windows Form.
Create a new Visual Basic Windows Application project by clicking the New project button in Microsoft Development Environment. Form1 is added to the project by default. Name the form as FrmView.
-Select View from the menu bar, and then select ToolBox from the View menu. The Toolbox is displayed.
-From the Windows Forms tab of the Toolbox, drag five label controls and five Textbox controls, to the form.
-Set the Text property of the label controls as displayed in the following table:
Drag four Button controls to the form. Set the Name and Text property for each button as displayed in the following table:
For steps on creating a connection, a data adapter, and a dataset using a wizard Click Here.
Make the following declaration at the Form level:
Dim bm as BindingManagerBase
In this example, a BindingManagerBase class object bm is declared. The object bm is initialized by using the BindingContext() method of the form. The position property of BindingManagerBase class is set to 0, which displays the first row of the DataTable Customer. You can change the value of the position property to specify the row of the DataTable to be bound to the controls.
You will use the BindingManagerBase class and the BindingContext() method to navigate through the records.
Code for the Load event of the form:
The statement SqlDataAdapter1.Fill(DsCust1) populates the dataset with the records from the Customers table.
Write the following code for navigating through the data.
The column names to be selected from the table Customers for the text property of each of the TextBox controls.
To view the bound data, select Debug from the menu bar. Then, select Start from the Debug menu. You can also run the application by clicking the Start icon on the standard toolbar.
Records from the Customers table are displayed. You can navigate through the records by using the First, next, and Previous buttons.
For every data source that is bound to a Windows Form control, there exists a CurrencyManager object. The CurrencyManager object handles the binding to the data source by keeping a pointer to the current item in the record list.
The CurrencyManager class is derived from the BindingManagerBase class. If all the Windows Form controls are bound to a single data source, the form will have one CurrencyManager object associated with it.
A BindingContext object, which is a Windows Form object, is used to keep track of the existing CurrencyManager objects in a form.
A Windows Form containing bound controls has at least one BindingContext object that keeps track of one or more CurrencyManager objects, each in turn keeping track of a data source. The figure given below illustrates the relationship between the BindingContext class, CurrencyManager class, and a Windows Form:

Fig:- 1.1. Relationship between the BindingContext class, CurrencyManager class and a Windows Form.
Design a Windows Form to display Data.
To design a Windows Form for displaying Data, retrieved from a SQL Server database, perform the following steps:Create a new Visual Basic Windows Application project by clicking the New project button in Microsoft Development Environment. Form1 is added to the project by default. Name the form as FrmView.
-Select View from the menu bar, and then select ToolBox from the View menu. The Toolbox is displayed.
-From the Windows Forms tab of the Toolbox, drag five label controls and five Textbox controls, to the form.
-Set the Text property of the label controls as displayed in the following table:
Label | Text Property |
---|---|
Label1 | Customer ID |
Label2 | Company Name |
Label3 | Contact Name |
Label4 | Address |
Label5 | City |
Drag four Button controls to the form. Set the Name and Text property for each button as displayed in the following table:
Button | Name | Text |
---|---|---|
Button1 | bnfirst | First |
Button2 | bnprev | Previous |
Button3 | bnnext | Next |
Button4 | bnlast | Last |
Connect to Database
Connecting to the NorthWind SQL Server database involves creating a connection, a data adapter, and a dataset. To connect to the SQL Server database, perform the following steps:-
Create a DataAdapter named SqlDataAdapter1 through the wizard.
- On the second screen of the wizard, click the New Connection button and establish a connection with NorthWind SQL Server database.
- Add the table Customers and select all its fields for the Select command in Query Builder.
- Generate a new Dataset DSCust1.
For steps on creating a connection, a data adapter, and a dataset using a wizard Click Here.
Make the following declaration at the Form level:
Dim bm as BindingManagerBase
In this example, a BindingManagerBase class object bm is declared. The object bm is initialized by using the BindingContext() method of the form. The position property of BindingManagerBase class is set to 0, which displays the first row of the DataTable Customer. You can change the value of the position property to specify the row of the DataTable to be bound to the controls.
You will use the BindingManagerBase class and the BindingContext() method to navigate through the records.
Code for the Load event of the form:
Dim bm As BindingManagerBase Private Sub Frmview_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load SqlDataAdapter1.Fill(DsCust1) bm = Me.BindingContext(DsCust1, "Customers") bm.Position = 0 End Sub
The statement SqlDataAdapter1.Fill(DsCust1) populates the dataset with the records from the Customers table.
Write the following code for navigating through the data.
Private Sub bnnext_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bnnext.Click bm.Position += 1 End Sub Private Sub bnlast_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bnlast.Click bm.Position = bm.Count - 1 End Sub Private Sub bnprev_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bnprev.Click bm.Position -= 1 End Sub Private Sub bnfirst_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles bnfirst.Click bm.Position = 0 End Sub
Bind the data to a Windows Form Control
Make the Text property under the Appearance category blank for all the TextBox controls. Expand the DataBinding property under the Data category. When the Text property is clicked, a down arrow is displayed. Clicking on the down arrow displays a list of datasets and the corresponding tables. The tables contain the existing column names.The column names to be selected from the table Customers for the text property of each of the TextBox controls.
To view the bound data, select Debug from the menu bar. Then, select Start from the Debug menu. You can also run the application by clicking the Start icon on the standard toolbar.
Records from the Customers table are displayed. You can navigate through the records by using the First, next, and Previous buttons.
Visual Basic.Net Articles
- access database in visual basic.net
- user custom controls in C# visual basic 2005, asp.net, application
- Using assemblies in .net applications
- How to create and sign a shared assembly
- Data access using ado .net in Visual Basic .Net
- Middle tier component for accessing sql server using visual basic .net
- Databinding | binding data to a datagrid control
- Dataset in Visual basic .net
- Ado.net data access
- Creating, Using Namespaces in visual basic.Net
- Business, Presentation and Data layers
- Implementing Structure in visual basic.Net
People also viewed:
Create your own ERP Software using ASP Core>>
Create your own Azure SOM Software>>
Create your own ERP Software using ASP .Net and SQL>>
Create your own Accounting Software using C# >>
Create your own SOM using Entity Framework MVC >>





Most Viewed
Azure Q & A
Azure Platform
Grid-View
GridView CommandField example
Details-View
GridView and DetailsView Master/Detail page using SqlDataSource control
POCO
POCO overview and advantages - POCO class, Entity Framework in Enterprise Applications
Entity Framework
Query entity data model using linq to entities
Array List
Difference between arraylist and list collection
Web Services
How to create a Web service using Visual Studio.net
Form-View
FormView DataBound Event
Object Oriented Programming
Calling base class constructor in C#
Linq
Convert a sequence to a generic list using ToList()method
Project Ideas
Project ideas for students
AccountingSoftware
Accounting Software
MVC
Creating an ASP.Net MVC 3 application
.Net
Using assemblies in .net applications
ASP .Net
How to implement form validation using ASP.Net 2.0 Validation Controls