How to Bind Data From a SqlDataReader to a Label in Asp.Net C# VB.Net

In many forums I found new developers often facing a problem to Bind SqlDataReader returned data into a Label Control. That’s why I am decided to address this issue in a most easiest way, so that they can easily understand the code as well as the process to implement in their projects.

To complete the Example create a sample table like below:

CREATE TABLE [dbo].[Article](
    [ArticleID] [int] NOT NULL,
    [Title] [varchar](max) NOT NULL,
    [IsPublished] [bit] NOT NULL,
([ArticleID] ASC)

Insert Some Data to Test:

INSERT INTO ARTICLE VALUES(1,'Populate SQL Data Into Checkboxlist',1)
INSERT INTO ARTICLE VALUES(2,'Checkboxlist data population example',0)
INSERT INTO ARTICLE VALUES(3,'C# Checkboxlist lesson',1)
INSERT INTO ARTICLE VALUES(4,'VB.Net Checkboxlist Example',0)
INSERT INTO ARTICLE VALUES(5,' Checkboxlist load data',0)

Now Modify the Web.Config file to prepare Connection String:

    <add name="DBConnection" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=Test;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 just add a Label Control like below:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

Now Bind Data From a SqlDataReader to a Label Control:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
                SqlCommand sqlComm = new SqlCommand("SELECT * FROM Article WHERE ArticleID=@ArticleID", conn);
                sqlComm.Parameters.Add("@ArticleID", SqlDbType.BigInt).Value = 1;

                SqlDataReader reader = sqlComm.ExecuteReader();
                if (reader.Read())
                    Label1.Text = reader["Title"].ToString();

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

VB.Net Code:

    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString)
                Dim sqlComm As New SqlCommand("SELECT * FROM Article WHERE ArticleID=@ArticleID", conn)
                sqlComm.Parameters.Add("@ArticleID", SqlDbType.BigInt).Value = 1

                Dim reader As SqlDataReader = sqlComm.ExecuteReader()
                If reader.Read() Then
                    Label1.Text = reader("Title").ToString()
                End If
            End Using
        End If
    End Sub

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

Sample Output:

If SqlDataReader returned multiple result set then you need to loop through whole DataReader to assign values into multiple Asp.Net Label Control. Here in this example I have ensured one row by using Primary Key in Where Clause

Download Code Example C#        Download Code Example VB.Net

Posted in .Net,, C#, VB.Net

Leave a Reply

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