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

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

Article:
Viewed:  2395 
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 asp.net. . . ? 

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

My SQL Server Table is:

1.png

Image 1.

Data inside table is:

2.png

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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Fill CheckBox List From DB</title></head>
<body>
    <form id="form1" runat="server">
    <div>
        <table cellpadding="4" cellspacing="4" width="70%" align="center" style="border: solid 1px #347C17;">
            <tr>
                <td style="background-color: #98AFC7; color: Green; font-weight: bold; font-family: Arial;"
                    align="center">
                    CheckBox From SqlServer Data Table
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:CheckBoxList ID="cblCourses" runat="server" RepeatColumns="4" BackColor="#FEFCFF"
                        Width="90%" ForeColor="#7F525D" CellPadding="2" CellSpacing="5">
                    </asp:CheckBoxList>
                </td>
            </tr>
            <tr>
                <td align="right">
                    <asp:Button ID="btnSelectCourse" runat="server" Text="Get Selected Course" OnClick="btnSelectCourse_Click"
                        Width="180px" />
                </td>
            </tr>
            <tr>
                <td align="center">
                    <asp:Label ID="lblSelectedCourse" runat="server" Font-Bold="true" Font-Size="12pt"
                        Font-Names="Verdana" ForeColor="#2B60DE" />
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>
 

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)
            bindAllCourse();
    } 

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

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

My Output: Checkbox List from DB

3.png

Image 3.

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


4.png

Image 4.

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