Create Auto Generate Serial No or Number in Asp.Net DataList Control using C# VB.Net

Providing Serial Number in tabular format rows is very useful for users. We can provide Serial number in DataList Control rows easily which increase more readability. Here in this article I will discuss “How we can Add Auto Serial Number in Asp.Net DataList Control using C# VB.Net

Create Auto Generate Serial No or Number in Asp.Net DataList Control using C# VB.Net

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 to Generate Auto Serial Number:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:DataList id="DataList1" runat="server">
                <b>SL No:</b>
                <asp:Label id="lblSLNO" runat="server" Text='<%# Container.ItemIndex+1 %>'></asp:Label><br />

                <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 Control:

    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.

Hope now you can Add a Serial Number for each DataList Rows.

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