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

Asp.Net ListBox Control is one of the most popular control. We need to Bind data in the control in many cases. SqlDataAdapter is one of the most popular DataSource and here in this article I will describe how to Bind Sql Server Database Data into the ListBox control using the SqlDataAdapter DataSource.

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)
            DataTable dt;
            String SQL = "SELECT * FROM Article";

            string sConstr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(sConstr))
                using (SqlCommand comm = new SqlCommand(SQL, conn))
                    using (SqlDataAdapter da = new SqlDataAdapter(comm))
                        dt = new DataTable("tbl");

            ListBox1.DataSource = dt;
            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
            Dim dt As DataTable
            Dim SQL As [String] = "SELECT * FROM Article"

            Dim sConstr As String = ConfigurationManager.ConnectionStrings("DBConnection").ConnectionString
            Using conn As New SqlConnection(sConstr)
                Using comm As New SqlCommand(SQL, conn)
                    Using da As New SqlDataAdapter(comm)
                        dt = New DataTable("tbl")
                    End Using
                End Using
            End Using

            ListBox1.DataSource = dt
            ListBox1.DataTextField = "Title"
            ListBox1.DataValueField = "ArticleID"
        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

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

Leave a Reply

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