How to Fill CheckBoxList from Sql Server table in,VB)

checkboxlist example in asp.netIntroduction: In one of my previous article i explained  Fill CheckBoxList based on DropDownList selection and How to get CheckBoxList selected items in comma separated format  and How to validate CheckBoxList using JavaScript in and  How to fill DropDownList from Sql server database in  and Fill ListBox with Sql Server Database in  and Bind RadioButtonList from Sql server table in

Now in this article I am going to explain how to dynamically Bind/Load/Fill CheckBoxList with the data from the Sql Server table. It  is one of very common requirement while working on application. 
  • In the design page(.aspx) place a DropDownList and a CheckBoxList control as:
         <asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="2">
  • Create a connectionstring in the web.config file under configuration tag as:
                                <add name="MyDbCon" connectionString="Data Source=LocalServer;Initial Catalog=MyDataBase;Integrated Security=True"/>

Note: Replace the Data Source and Initial Catalog(i.e. Database name) as per your application.
  •  Create a Database  e.g. "MyDataBase" in sql server and also create a table  as shown below  and name it  "Qualification_Tb"

  • Now in the code behind file(.aspx.cs) write the code as 
Asp.Net C# Code to Bind/Load/Fill CheckBoxList from Sql server Database in

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
protected void Page_Load(object sender, EventArgs e)
if (!Page.IsPostBack)

private void FillQualCheckBoxList()
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyDbCon"].ConnectionString);
        SqlCommand cmd = new SqlCommand("Select * from Qualification_Tb", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        cblCourses.DataSource = dt;
        cblCourses.DataTextField = "Qualification";
        cblCourses.DataValueField = "Qualification_Id_Pk";

Asp.Net VB Code to Bind/Load/Fill CheckBoxList from Sql server Database in
  • Now in the code behind file(.aspx.vb) write the code as:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
        End If
    End Sub

Private Sub FillQualCheckBoxList()
        Dim con As New SqlConnection(ConfigurationManager.ConnectionStrings("MyDbCon").ConnectionString)
        Dim cmd As New SqlCommand("Select * from Qualification_Tb", con)
        Dim adp As New SqlDataAdapter(cmd)
        Dim dt As New DataTable()
        cblCourses.DataSource = dt
        cblCourses.DataTextField = "Qualification"
        cblCourses.DataValueField = "Qualification_Id_Pk"
    End Sub

Now over to you: 
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Next Post »


Click here for comments
September 26, 2013 ×

There is no need to write any code-behind for this as this would do exactly the same:
<asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="2" DataSourceID="sdsCourses" DataValueField="Qualification_Id_Pk" DataTextField="Qualification" />
<asp:SqlDataSource ID="sdsCourses" runat="server" ConnectionString="<%$ ConnectionStrings:MyDbCon %>" SelectCommand="SELECT * FROM Qualification_Tb;" />

September 26, 2013 ×

Hello andrec..yes you are right..that's also a way to fill the checkbox using SqldataSource..thanks for sharing your knowledge too..keep reading. :)


If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily..