Bind SQL Server Table Data into DataList Control in Asp.Net C# VB.Net

The DataList control is a standard control of Asp.Net like others. It is used to display repeated list of data or items. You can bind any datasource in this DataList control like DataTable, SqlDataReader and XML etc. In most e-commerce site developers use DataList control to display products insted of Repeater control or GridView Control. Here in this example i will show you how one can bind data to a DataList control in run time or code behind.

To do the Example First Create a SQL Server Database Table:

CREATE TABLE [dbo].[tblStock](
    [BrandName] [varchar](max) NULL,
    [CategoryName] [varchar](max) NULL,
    [ProductName] [varchar](max) NULL,
    [LogicalQuantity] [bigint] NULL,
    [PhysicalQuantity] [bigint] NULL,
    [QuarentineQuantity] [bigint] NULL

Now Insert Some Data:

INSERT INTO tblStock VALUES('Kohinoor','Soap','Tibbet Handwash',55781,55780,1)
INSERT INTO tblStock VALUES('Kohinoor','Soap','Tibbet Soap',38786,38780,6)
INSERT INTO tblStock VALUES('Kohinoor','ToothPaste','Tibbet Clear',34503,34500,3)
INSERT INTO tblStock VALUES('Proctor & Gamble','Bubble','',43809,43800,9)
INSERT INTO tblStock VALUES('Telenor','Mobile','Data SIM',43876,43870,6)
INSERT INTO tblStock VALUES('Telenor','Mobile','GP Modem',23907,23900,7)
INSERT INTO tblStock VALUES('Uniliver','Soap','Lux',19806,19800,6)
INSERT INTO tblStock VALUES('Uniliver','TeleProducts','Brittle',20703,20700,3)
INSERT INTO tblStock VALUES('Uniliver','ToothPaste','Close Up',16755,16750,5)

Now modify the Web.Config File to Connect to Database:

    <add name="DBConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TESTDB;Trusted_Connection=yes;" providerName="System.Data.SqlClient"/>
    <!--<add name="BONConnection" connectionString="Data;Initial Catalog=DBNAME;User Id=UserName;Password=YourPassword;" providerName="System.Data.SqlClient" />-->

Note: If you use windows authentication then use the first key. Otherwise use second key line. Update server name, Database name, User name, Password as per your settings.

Now add a DataList Control Like Below:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:DataList id="DataList1" runat="server">
                <asp:Label id="lblBrandName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "BrandName") %>'></asp:Label><br />

                <asp:Label id="lblCategoryName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "CategoryName") %>'></asp:Label><br />

                <asp:Label id="lblProductName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "ProductName") %>'></asp:Label><br />
                <asp:Label id="lblHeader" runat="server" Font-Names="Tahoma" ><h1>List of Products:</h1></asp:Label>
                <hr />
                <hr />
                <hr />

Now Bind DataList Control under Page_Load Event:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            string consString = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(consString))
                SqlCommand cmd = new SqlCommand("SELECT TOP 5 * FROM [tblStock]", conn);
                cmd.CommandType = CommandType.Text;
                SqlDataReader drStock = cmd.ExecuteReader();
                DataList1.DataSource = drStock;

Note: Don’t forget to add “System.Data”, “System.Data.SqlClient”, “System.Configuration” namespaces.

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim consString As String = ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString
            Using conn As New SqlConnection(consString)
                Dim cmd As New SqlCommand("SELECT TOP 5 * FROM [tblStock]", conn)
                cmd.CommandType = CommandType.Text
                Dim drStock As SqlDataReader = cmd.ExecuteReader()
                DataList1.DataSource = drStock
            End Using
        End If

    End Sub

Note: Don’t forget to add “System.Data”, “System.Data.SqlClient”, “System.Configuration” namespaces.

Now run the Project. Outcome:
Bind SQL Server Table Data into DataList Control in Asp.Net Csharp VB.Net

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code