In this article I will explain with an example, how to bind (populate) ASP.Net CheckBoxList using DataTable (DataSet) in C# and VB.Net.
The data from the SQL Server Database Table will be fetched into a DataSet and then the DataTable of the DataSet will be used to bind (populate) ASP.Net CheckBoxList using C# and VB.Net.
This article will also illustrate how to fetch the selected values of ASP.Net CheckBoxList on Button Click using C# and VB.Net.
 
 
Database
This article makes use of a table named Fruits whose schema is defined as follows.
Bind (Populate) ASP.Net CheckBoxList using DataTable (DataSet) in C# and VB.Net
 
The Fruits table has the following records.
Bind (Populate) ASP.Net CheckBoxList using DataTable (DataSet) in C# and VB.Net
 
Note: You can download the database table SQL by clicking the download link below.
          Download SQL file
 
 
HTML Markup
The following HTML Markup consists of an ASP.Net CheckBoxList control and a Button.
<asp:CheckBoxList ID="chkFruits" runat="server">
</asp:CheckBoxList>
<br />
<asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Submit" />
 
 
Namespaces
You will need to import the following namespaces.
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
 
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
 
 
Populating CheckBoxList control using DataTable (DataSet) in ASP.Net
Inside the Page Load event of the page, the CheckBoxList is populated with the records of the Fruits Table.
The FruitName and the FruitId column values are fetched from the database into DataTable (DataSet) and are assigned to the DataTextField and DataValueField properties of the CheckBoxList control.
DataTextField – The values of the Column set as DataTextField are visible to the user.
DataValueField – The values of the Column set as DataValueField are not visible to the user. Generally ID or Primary Key columns are set as values in order to uniquely identify a CheckBoxList Item.
C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("SELECT FruitName, FruitId FROM Fruits"))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
                {
                    DataSet ds = new DataSet();
                    sda.Fill(ds);
                    DataTable dt = ds.Tables[0];
                    chkFruits.DataSource = dt;
                    chkFruits.DataTextField = "FruitName";
                    chkFruits.DataValueField = "FruitId";
                    chkFruits.DataBind();
                }
            }
        }
    }
}
 
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Using con As New SqlConnection(constr)
            Using cmd As New SqlCommand("SELECT FruitName, FruitId FROM Fruits")
                cmd.CommandType = CommandType.Text
                cmd.Connection = con
                Using sda As New SqlDataAdapter(cmd)
                    Dim ds As New DataSet()
                    sda.Fill(ds)
                    Dim dt As DataTable = ds.Tables(0)
                    chkFruits.DataSource = dt
                    chkFruits.DataTextField = "FruitName"
                    chkFruits.DataValueField = "FruitId"
                    chkFruits.DataBind()
                End Using
            End Using
        End Using
    End If
End Sub
 
 
Getting the Selected Text and Value of the ASP.Net CheckBoxList
When the Submit Button is clicked, the Value and the Text part of the Selected Item of the ASP.Net CheckBoxList is fetched and displayed using JavaScript Alert message box.
C#
protected void Submit(object sender, EventArgs e)
{
    string message = "";
    foreach (ListItem item in chkFruits.Items)
    {
        if (item.Selected)
        {
            message += "Value: " + item.Value;
            message += " Text: " + item.Text;
            message += "\\n";
        }
    }
 
    ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + message + "');", true);
}
 
VB.Net
Protected Sub Submit(sender As Object, e As EventArgs)
    Dim message As String = ""
    For Each item As ListItem In chkFruits.Items
        If item.Selected Then
            message &= "Value: " & item.Value
            message &= " Text: " & item.Text
            message &= "\n"
        End If
    Next
 
    ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & message & "');", True)
End Sub
 
 
Screenshot
Bind (Populate) ASP.Net CheckBoxList using DataTable (DataSet) in C# and VB.Net
 
 
Demo
 
 
Downloads