Introduction
In ASP.NET, the term Data Binding means binding the controls to data that is retrieved from the data source and providing a read or write connectivity between these controls and the data, that they are bound to.
These data sources can be one of databases, xml files or even, flat files. In ASP.NET 1.x, you were introduced to a powerful data binding technique where you could eliminate the need of writing lengthy code that was used in earlier for binding data to data controls.
In its simplest form, the syntax for using data binding in your ASPX pages is as follows:
<%# Data Source Name %>
Main
The following are the advantages of using Data Binding expressions in ASP.NET controls in the presentation layer:
- Flexibility to use any data binding expressions provided that the value it resolves to is one that the data control can use
- You can use these expressions to bind any property to its corresponding data.
- Flexibility to bind one property to one data source and another property to another data source. Y
You should use data binding in the ASP.NET web pages in the presentation layer of your application. The syntax used for data binding in ASP.NET 1.x is as follows:
<%# Container.DataItem("expression") %>The following code snippet illustrates how you can bind data to a label control using the syntax shown above:
<asp:Label id="lblUserName" runat="server"
Text='<%# Container.DataItem("UserName") %>'>
</asp:Label>You can also use the static method
Eval() of the DataBinder class for binding data to your controls. This method has an overloaded version that accepts the format expression as an additional parameter that relates to the type of formatting that you would require on the data to be displayed. The syntax for using the
Eval()method is shown as follows:
<%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>
As shown in the code snippet the
Eval()method accepts two parameters:
- The first of these parameters is the data container, that is, a data table, a data set or a data view.
- The second parameter is a reference to the value that needs to be assigned to the control.
Refer to the above code snippet, the equivalent way of binding the data is as follows.
<asp:Label id="lblUserName" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "UserName") %>'>
</asp:Label>
You can use the overloaded version of the Eval() method to specify the format expression (as an additional optional parameter) to display the data in your required format. Refer to the following code snippet:
<asp:Label id="lblLoginDate" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "DateRegistered", "{0:dddd
d MMMM"]) %>'>
</asp:Label>With ASP.NET 2.0, you have a much simpler syntax as the DataBinder instance is now the default context for all data binding expressions that are used for displaying non-hierarchical data in your presentation layer. You can now use any of the following overloaded versions of the Eval() method for binding data.
<%# Eval("expression") %>
<%# Eval("expression"[, "format"]) %>Refer to code snippet above. the equivalent way of binding data in ASP.NET 2.0 is as follows : >
<asp:Label id="lblLoginDate" runat="server"
Text='<%# Eval("DateRegistered", "{0:dddd
d MMMM"]) %>'>
</asp:Label>New Data Source Controls in ASP.NET 2.0
With ASP.NET 2.0, data binding has been simplified a lot with the introduction of a number of data source controls. These data source controls are server controls that can be used to bind data to a number of data sources.
In ASP.NET 2.0, you have the following new data source controls which are listed below
- Object data source control: This control can be used to bind data to middle-tier objects to the presentation layer components in an N-tier design.
- SQL data source control: This control enables you to connect to and bind data to a number of underlying data sources, that is, Microsoft SQL Server, OLEDB, ODBC or Oracle databases.
- Access data source control: This control can be used to bind data to Microsoft Access databases.
- Xml data source control: This control can be used to bind data to XML data sources, that is, external XML data files, dataset instances, etc.
Using the ObjectDataSource Control
Before we use the object data Source control, we need to create the object that later on we will bind into the object data source control. Create New Web Site in VS 2008 and add new cs file below.
public class DataManager
{
public ArrayList GetEmployeeName()
{
ArrayList arrList = new ArrayList();
arrList.Add("James");
arrList.Add("Jim");
arrList.Add("John");return arrList;
}
}
To use the object data source control :
1. Open the default.aspx file and then switch to design view mode.
2. Now, add the ObjectDataSource control by dragging it from the toolbox. An ObjectDataSource
control with the default name of ObjectDataSource1 is added to the web page
3. Drag GridView control and add it into the default page.
4. The next step is to configure the ObjectDataSource control.
5. We associate the Objectdatasource control to the GridView Control and set its DataSource
property to the ObjectDataSource Control that we have added in our web page. The following
screenshot illustrates how we associate the data source for the GridView control to our
ObjectDataSource control.

6.Now it is the time to confiigure the ObjectDataSource control and Choose the business object as you
can see in the screenshot below

7. After this, you can configure the Select methods and choose the GetEmployeeName() Method
from the drop down.
8. Run the website and you will notice that the GridView control will display the result.
Using the SQLDataSource Control
Built on top of ADO.NET, the SQL data source control is a non-visual control and uses the built in ADO.NET objects for its operation. The SQL data source control is used to access data from any relational database, SQL Server database in particular. You can follow some simple steps described below that can be used to connect to your database and perform your Insert, Update, Select , Delete operations in your applications with minimal or no coding at all
The SQLDataSource control is quite easy to use. And I will not demonstrate on how to use it in this article. Basically you just need to Select the Database, Choose the Table or view you want to view and the sql will be generated automatically for you. It was designed so easy so that you don't have to code any code or sql at all.
Using the AccessDataSource Control
The Access Data Source control also is similar to the SQL DataSource control. The only difference is that
SQLDataSource Control can be used to connect to SQL Server Database while the AccessDataSource control can only be used with Microsoft Access Database.
Using the XMLDataSource Control
The XML data source control introduced in ASP.NET 2.0 can be used to bind to an XML Document seamlessly. It can also be used to bind hierarchical data with data controls that supports it.
Before we start coding the XMLDataSource control, we need to prepare the XML document that later on will be bind into the XMLDataSource Control.
<Employees>
<Employee ID="1" Name ="James" >
<Employee ID="2" Name ="Jimmy" />
<Employee ID="3" Name ="Johny" />
<Employee ID="4" Name ="Johnson" />
<Employee ID="5" Name ="Jack" />
<Employee ID="6" Name ="Jill" />
<Employee ID="7" Name ="Jolie" />
</Employees>
We now need a data control for displaying the data that would be retrieved by the XML data source control. For this, drag and drop a GridView control from the toolbox and associate the data source of this control with the XMLDataSource control created and configured earlier. Refer to the following screenshot:
Configure the DataSource of the XMLDataSource as the screenshot below
Once you configure the XMLDataSource, you are ready to go. Run the web project and you can see the GridView will display the table with the record from the XML File.
Conclusion
As you can see that data binding in asp.net 2 is much more easier than asp.net 1. Microsoft has released all this data binding control to make life of developers easy. Thats all I want to say.
Hope you like this article