How to Bind Data to ListBox Control using SqlDataReader in ASP.NET C# VB.Net

Asp.Net ListBox Control is one of the most useful control. In many cases we need to implement this control. Before doing anything at first we need to bind data into the ListBox control. Here in this article I will describe how to Bind Sql Server Database Data into the ListBox control using SqlDataReader. Since SqlDataReader is one of the most popular datasource in Asp.Net.

How to Bind Database Data to ListBox Control in ASP.NET Csharp

To Do the Example We need to create a Database Table First:

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

Now we need to Insert some Data:

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 need to add a ListBox Control:

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ListBox ID="ListBox1" runat="server" SelectionMode="Multiple"></asp:ListBox>

Now we need to Bind Data into ListBox Control:
C# Code:

    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString))
                SqlCommand cmd = new SqlCommand("SELECT * FROM [Article]", conn);
                cmd.CommandType = CommandType.Text;
                SqlDataReader drStock = cmd.ExecuteReader();
                ListBox1.DataSource = drStock;
                ListBox1.DataTextField = "Title";
                ListBox1.DataValueField = "ArticleID";

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
            Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString)
                Dim cmd As New SqlCommand("SELECT * FROM [Article]", conn)
                cmd.CommandType = CommandType.Text
                Dim drStock As SqlDataReader = cmd.ExecuteReader()
                ListBox1.DataSource = drStock
                ListBox1.DataTextField = "Title"
                ListBox1.DataValueField = "ArticleID"
            End Using
        End If

    End Sub

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

Hope now you can bind ListBox to Database data from server side or code behind.

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