ASP.Net 2.0 DataList DataBinding using C#

Updated on 08 Aug 2012,
Published on 03 Apr 2009

ASP.Net DataList Databound Control’s Databinding feature displays the data items retrieved from DataSource in the specified layout and appearance style for ItemTemplate. Here we will discuss it using ASP.Net C# code to connect with SQL Server Database and DataList control databinding using its DataSource property that gets or sets the data provided by the data source for populating the list items inside the ItemTemplate. In the previous series of tutorials about ASP.Net DataList Control we discussed about the layout templates that can be used to customize the layout of DataList DataBound Control. ItemTemplate field is the required template field to display the records retrieved from the DataSource. You can specify the common title for the items in the HeaderTemplate. Text specified in the HeaderTemplate does not follow the repeat behavior while displaying more than 1 list item in each row. It renders only once.

In this DataList Control Tutorial we will use Northwind SQL database to bind the data and display its specified fields in the ItemTemplate. You can specify the following connectionstring code inside the ConnectionStrings section of web.config file:

    <add name="NorthwindConnectionString" 
         connectionString="Data Source=.\SQLEXPRESS; Initial Catalog=Northwind; User ID=sa"
         providerName="System.Data.SqlClient" />

It will provide the connection for Northwind database. While working on real time project you will have to specify the password in the connection string. You can use “Password=YourSecretPassword” after User ID property of string.

HTML Code for DataList Control and DataBinder.Eval Function

        Product Catalog
         <%# DataBinder.Eval(Container.DataItem, "ProductName") %>
         <%# DataBinder.Eval(Container.DataItem, "UnitPrice") %>
    <HeaderStyle Font-Bold="true" Font-Names="Arial" />
    <ItemStyle Font-Names="Arial" Font-Size="Small" />

In the above code template for DataList control DataBinder class method has been used to access the value of specified data item. DataBinder class is a non-inheritable class that provides the support for Rapid application development designers to generate and parse the expression syntax for substituting the values before the page loads. Eval method of DataBinder class accepts two types of parameters. Its first parameter passes the object reference for which the string expression specified as the second parameter is to be evaluated.

<%# DataBinder.Eval(Container.DataItem, "ProductName") %>

Above expression syntax has been used in the DataList control’s ItemTemplate to display the value of ProductName field of products table of Northwind database.

ASP.Net C# Code for DataList Databinding using DataSource

// string variable to store the connection string
// retrieved from the connectionStrings section of web.config
string connectionString = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString;

// sql connection object
SqlConnection mySqlConnection = new SqlConnection(connectionString);

// sql command object initialized with select command text
SqlCommand mySqlCommand = new SqlCommand("select productname, unitprice, productid from products", mySqlConnection);

// check the connection state and open it accordingly.
if (mySqlConnection.State == ConnectionState.Closed)

// Sql datareader object to read the stream of rows from SQL Server Database
SqlDataReader myDataReader = mySqlCommand.ExecuteReader();

// Pass the Sql DataReader object to the DataSource property
// of DataList control to render the list of items.
DataList1.DataSource = myDataReader;

// close the Sql DataReader object

// check the connection state and close it accordingly.
if (mySqlConnection.State == ConnectionState.Open)

Above code will generate the output result as follows:

You can also perform the task using VB.Net code for ASP.Net DataList Databinding. We will use the same HTML code template of DataList control but will write new code for data binding using VB.Net.

Continue to next tutorial: ASP.Net 2.0 DataList Select Item using C# to learn how to select an item of datalist control.

0 Responses to "ASP.Net 2.0 DataList DataBinding using C#"
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email