Exforsys.com
 
Home Tutorials VB.NET 2005
 

Simple Data Binding

 

Simple Data Binding

Page 1 of 3

VB.NET 2005 Tutorials: Simple Data Binding

In Section 1 of Data Binding you will learn about definition of Data Binding Bindable Entities, The Architecture of Data Binding, Bind Data to the User Interface and Simple Data Binding



Data Binding

The process of binding a control to a data source is called data binding. Visual Studio 2005 includes several new features to assist in developing applications that access data. The Data Source Configuration Wizard simplifies connecting the application to data in databases, Web services, and user-created objects. The new Data Sources window provides a centralized view of the data available to the project, and it reduces the complexity of data binding by allowing the user drag items from the window onto forms to create data-bound controls. Filling datasets, running queries, and executing stored procedures can be accomplished using the new Visual Studio-generated TableAdapter object. The new local data feature allows the user include Microsoft SQL Express and Microsoft Access database files, directly in to the application.


Windows Forms can take advantage of two types of data binding: simple binding and complex binding. Each offers different advantages. We will look at them a little later in this lesson.


Bindable Entities:

Binding data to form controls allows the user access data from databases as well as data in other structures, such as arrays and collections which support IList interface. The data providers for other objects in .NET Frameworkare listed below:


  • Array or collection that implement IList interface.
    .

  • ADO .NET objects
    • DataColumn object. The users can simple-bind a control (such as a TextBox control's Text property) to a column within a data table.
      .

    • DataTable object. The user can complex-bind a control to the information contained in a data table (such as binding the DataGrid control to a data table) using the default view of the DataTable.
      .

    • DataView object. The users can simple- or complex-bind to the data within a data view. However this provides a fixed snapshot of the data.
      .

    • DataSet object. The users can simple- or complex-bind to the data within a dataset using the default view.
      .

    • DataView Manager Object. It functions like DataView, with a difference that this snapshot comes with relationships that exists between the tables as seen in a DataSet.

The Architecture of Data Binding

  • Create Connection Object. The connection that the users create, forms the basis for all other activities. In doing so the user creates a connect string and also a connection object that provides connection to the database.
    .

  • Create DataSet and DataAdapter. Then the user proceeds to set the Data Binding property of the text box. At this stage the Visual Studio creates a DataSet and a DataAdaptor. The “Select ... “ statement for creating the DataSet is also autogenerated based on the selections the user has made in the Data Source configuration wizard.
    .

  • Call the fill method of the DataAdapter to fill the dataset. This also adds a line of code that fills the dataset using the data adapter and the connection object . At the end of it all the data from the selected data source is available for the control when the form loads.

Having said all that, let us now see how simple binding and complex binding of data to controls is accomplished.


Bind Data to the User Interface

Simple Data Binding

Simple data binding is the ability of a control to bind to a single data element--such as a value in a column in a dataset table. This is the type of binding typical for controls typically display a single value such as a System.Windows.Forms.TextBox control or System.Windows.Forms.Label control. In fact, any property on a control can be bound to a field in a database. Let us create a sample to understand the concept better.


1. Create a new Windows Application project in Visual Basic 2005 IDE .


2. Press Ctrl + Alt + S to open the Data Explorer if it is not seen docked to the top left hand side corner of the window. The screenshot below shows the Database Explorer.



3. Click on the “Connect to Database” icon on the left window pane, to open the dialog box. Choose option Database. In the dialog box “Choose Data Source”, select Microsoft SQL Server Database File and click continue as shown below:



4. The “Add Connection” dialog box opens. Click on the Browse button to choose the Database file. Now click on “Test Connection”. The user will get a message box saying “Connection Successful”. Click on OK to add the database.



5. The user will see a new connection added in the database explorer:



6. Add a TextBox to the form and also a button which we will use for closing the application. The window will look like the screenshot shown below:




7. Add a BindingNavigator from the toolbox to the form. In the property window set the value for property ‘Dock as Bottom’. Set the Text property of the Button1 to Exit. Add the following code to the click event of the button.

Me.Close()

The window will look like the screenshot shown below:



Next Page: Simple Data Binding - Page 2


Read Next: .NET Complex Data Binding



 

 

Comments


lonrey said:

  This VB.NET 2005 Tutorial on Simple Data Binding was very helpful.
It allowed me to get a simple database program up and running from nothing in about half an hour.
I didn\'t have to refer to any other reference material.
Great job.by the author.
July 16, 2006, 9:05 am

felix said:

  I can't seem to drag the Data Set into the form and so the DataGrid like structure does not appear in my form. Any suggestions to solve this problem? Thanks
January 16, 2007, 7:36 pm

Flakky said:

  Was very clear and simple tutorial, Easy to implement with satisfying results.

Had some problems with getting to the right screens
(for example Data Source floating window) but this is just my lack of knowledge with the Visual Studio IDE

Good Job :X
January 25, 2007, 9:15 am

Senthil Babu said:

  It is really good for the beginners to understand the steps and working out to get the result.
Thanks.
March 12, 2007, 4:33 am

Punith Kumar said:

  Thanks,
April 5, 2007, 2:37 am

SQL Newby said:

  This was really helpful, but the Data Source dragging bit was a little unclear. For those like myself....
In the "Data" menu, select "Show Data Sources"
The "Data Sources" window opens. Highlight the table from the Data Set you want and drag that table to the form you want to use it on. If you don't want to actually see the table, just turn the Visible property to False. This changes some code automatically for you, so that if you add more text boxes to display your information, they are all linked. Ok, awesome!!! Happy coding =)
June 21, 2007, 1:59 pm

Mohit Bhai Agra Wale said:

  Its very helpful for developer and its good for all users.Thanks
January 18, 2008, 12:57 am

abhineet komal said:

  This is what i was trying for: thanx,plz put some same kind of programs like this
July 15, 2008, 5:17 am

Rajmohan said:

  very useful website. Easy to learn.Thanks
September 23, 2008, 4:56 am

santosh said:

  after data binding connection,we want enter new data add button with click event please send code how to add data
November 10, 2008, 3:13 am

Chris said:

  In effect it is very userful for beginners but it does not adress typical problems developers have. e.g. normalized data tables with FK etc. How can i represent this in a control (getting data from master table instead of FK) and how can i save it? Because DataTableAdpater doesn't create update command when table uses joins......
October 16, 2009, 5:38 am

rover said:

  I already tried this one, what i am looking is this scene:

I have a Datagridview, by adding, editing and deleting its contents, the database connected to it will also update. Using the databinding through codes.
December 21, 2009, 10:26 am

rover said:

  This method is only applicable if the database that will use by the application is directly specified. What if the user is the one that will specify the database using dialogbox? Display its contents in a datagridview and manipulate it just like in this method?

Could you please send the codes . . . . . .
December 21, 2009, 10:42 am

anum said:

  after data binding connection,we want enter new data add button with click event please send code how to add data..
I already tried this one, what i am looking is this scene:

I have a Datagridview, by adding, editing and deleting its contents, the database connected to it will also update. Using the databinding through codes.
December 29, 2009, 3:18 am

Oybek said:

  Thanks, I understand your method and I can bind simple databases. But I have 10 or 15 tables in a base and their binding another tables. I want to bind all tables and I want to select, insert, delete and update on tables.
Please, give me simple code bind to MS Access
December 30, 2009, 1:29 pm

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2010 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape