Email: Password:       Forgot Password 
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.

How to Fill CheckBoxList from Sql Server table and Get Selected CheckBox in in

Viewed:  3671 
Posted On:  8/15/2013 2:47:11 AM 
How we can bind check box list from SQL Server table and how we can get selected check box item in . . ? 

In this article I am going to show how we can fill or bind check box list from SQL Server table in C#.

My SQL Server Table is:


Image 1.

Data inside table is:


Image 2.

My ASPX is:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
<title>Fill CheckBox List From DB</title></head>
    <form id="form1" runat="server">
        <table cellpadding="4" cellspacing="4" width="70%" align="center" style="border: solid 1px #347C17;">
                <td style="background-color: #98AFC7; color: Green; font-weight: bold; font-family: Arial;"
                    CheckBox From SqlServer Data Table
                <td align="center">
                    <asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="4" BackColor="#FEFCFF"
                        Width="90%" ForeColor="#7F525D" CellPadding="2" CellSpacing="5">
                <td align="right">
                    <asp:Button ID="btnSelectCourse" runat="server" Text="Get Selected Course" OnClick="btnSelectCourse_Click"
                        Width="180px" />
                <td align="center">
                    <asp:Label ID="lblSelectedCourse" runat="server" Font-Bold="true" Font-Size="12pt"
                        Font-Names="Verdana" ForeColor="#2B60DE" />

My ASPX.cs is:

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!Page.IsPostBack)

    private void bindAllCourse()
        SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=Test;Trusted_Connection=yes;");
        SqlCommand cmd = new SqlCommand("Select * from TBL_Course", con);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        cblCourses.DataSource = dt;
        cblCourses.DataTextField = "CourseName";
        cblCourses.DataValueField = "CourseID";

    protected void btnSelectCourse_Click(object sender, EventArgs e)
        string values = "";
        foreach (ListItem objItem in cblCourses.Items)
            if (objItem.Selected)
                values += objItem.Text + ",";

My Output: Checkbox List from DB


Image 3.

If you want to know which course user selected then Select Check box and click on Get Selected Course Button


Image 4.

         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us