Please disable your adblock and script blockers to view this page
 

How to Fill CheckBoxList from Sql Server table in asp.net(C#,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 Asp.net and  How to fill DropDownList from Sql server database in asp.net  and Fill ListBox with Sql Server Database in asp.net  and Bind RadioButtonList from Sql server table in asp.net

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 asp.net application. 
  • In the design page(.aspx) place a DropDownList and a CheckBoxList control as:
         <asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="2">
        </asp:CheckBoxList> 
  • Create a connectionstring in the web.config file under configuration tag as:
<configuration>
<connectionStrings>
                                <add name="MyDbCon" connectionString="Data Source=LocalServer;Initial Catalog=MyDataBase;Integrated Security=True"/>
</connectionStrings>
</configuration>

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 asp.net

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

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();
        adp.Fill(dt);
        cblCourses.DataSource = dt;
        cblCourses.DataTextField = "Qualification";
        cblCourses.DataValueField = "Qualification_Id_Pk";
        cblCourses.DataBind();       
    }

Asp.Net VB Code to Bind/Load/Fill CheckBoxList from Sql server Database in asp.net
  • 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
            FillQualCheckBoxList()
        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()
        adp.Fill(dt)
        cblCourses.DataSource = dt
        cblCourses.DataTextField = "Qualification"
        cblCourses.DataValueField = "Qualification_Id_Pk"
        cblCourses.DataBind()
    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."
Previous
Next Post »

2 comments

Click here for comments
andrec
admin
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;" />

Reply
avatar
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. :)

Reply
avatar

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.. ConversionConversion EmoticonEmoticon