csharp

How to Bind CheckBoxList from Database in ASP.Net C#

Here Laxmikant has explained How to Bind/Populate CheckBoxList from Database in ASP.Net C#.
In this blog, we will learn step by step How to Bind/populate CheckBoxList from Database in ASP.Net C#.
Before Bind CheckBoxList from Database in ASP.Net C#. We will Learn about some basic knowledge of CheckBoxList Control
CheckBoxList:

  • CheckboxList is just like DropDownList. CheckBoxList is a collection of listItem Object.
  • It provides facility to a user can select multiple choice Like Hobbies, Qualification.
  • Some Important Properties of CheckBoxList Control

  • RepeatColumn
  • SelectedItem
  • SelectedIndex
  • Enabled
  • RepeatDirection
  • SelectedValue
  • We can learn in this article two way to items can be added in CheckBox list control
    First Way:
    How to Bind CheckBoxList from Database in ASP.Net C#.
    Second Way:
    Items can be added at design time(Html Source).
    Now Start, How to Bind CheckBoxList from Database in ASP.Net C#.Follow My Step
    Database
    For this blog i have created a Database and Simple Table with the two column “HobbiesId “, “HobbiesName “.
    [php]
    create database CheckBoxList_Demo
    use CheckBoxList_Demo

    create table tblHobbies(
    HobbiesId int primary key identity(1,1),
    HobbiesName varchar(40)
    )
    [/php]
    Insert record into Table “tblHobbies”.
    [php]
    insert into tblHobbies(HobbiesName)values(‘Cricket’),(‘Dancing’),(‘Reading’),(‘Swimming’),(‘Singing’),(‘Comedy’)
    [/php]
    Create Stored procedure for Get Record from Database.
    [php]
    create proc sp_tblHobbies_Select
    as
    begin
    select * from tblHobbies
    end
    [/php]
    Now Open Visual Studion and create the project as:
    “Start” –> “All Programs”- “Microsoft Visual Studio 2012”.
    “File” – “New Project” – “C#” – “Empty Project” (to avoid adding a master page).
    Give the Project name such as “CheckBoxList_Demo” or another as you wish and specify the location.
    Then right-click on Solution Explorer – “Add New Item” – “Default.aspx” page.

    Open Default.aspx page and write this code.
    In “Default.aspx” page drag and drop one of an ASP.Net CheckBoxList control .Open “Default.aspx” page put this code.
    Default.aspx/HTML
    [php]
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CheckBoxList_Demo.Default" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>Select Hobbies:</td>
    <td>
    <asp:CheckBoxList ID="cblhobbies" runat="server" RepeatColumns="3"></asp:CheckBoxList></td>

    </tr>

    </table>
    </div>
    </form>
    </body>
    </html>

    [/php]

    Open web.config file and Establish a connection with database

    Key Point:

    • In initial catalog write Database Name
    • In data source you can simply write (.) /your server name/(local)
    • Integrated security for Authentication

    [php]
    <connectionStrings>
    <add name="DBCS" connectionString="data source=.;initial catalog=CheckBoxList_Demo;integrated security=true"/>
    </connectionStrings>
    [/php]

    Open “Default.aspx.cs” page to write code for How to Bind CheckBoxList from Database in ASP.Net C#.
    Default.aspx.cs
    [php]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    //Need to import the following Namespaces
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;

    namespace CheckBoxList_Demo
    {
    public partial class Default : System.Web.UI.Page
    {
    //Establish Connection
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    //The BindHobbies() method is called up in the page load event.
    BindHobbies();
    }

    }

    //Create one method "BindHobbies" for populate hobbies from Database
    public void BindHobbies()
    {
    con.Open();
    SqlCommand cmd = new SqlCommand("sp_tblHobbies_Select", con);
    cmd.CommandType = CommandType.StoredProcedure;
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    if (ds.Tables[0].Rows.Count > 0)
    {
    cblhobbies.DataValueField = "HobbiesId"; //"HobbiesId" is the tblHobbies Column Name
    cblhobbies.DataTextField = "HobbiesName";//"HobbiesName" is the tblHobbies Column Name
    cblhobbies.DataSource = ds;
    cblhobbies.DataBind();
    }
    }
    }
    }
    [/php]

    Scrrenshot Describe the Output

    Second Way:
    Items can be added at design time(Html Source).
    Default.aspx
    [php]
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="CheckBoxList_Demo.Default" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <table>
    <tr>
    <td>Select Hobbies:</td>
    <td>
    <asp:CheckBoxList ID="cblhobbies" runat="server" RepeatColumns="3">
    <asp:ListItem Value="1" Text="Cricket"></asp:ListItem>
    <asp:ListItem Value="2" Text="Dancing"></asp:ListItem>
    <asp:ListItem Value="3" Text="Reading"></asp:ListItem>
    <asp:ListItem Value="4" Text="Swimming"></asp:ListItem>
    <asp:ListItem Value="5" Text="Singing"></asp:ListItem>
    <asp:ListItem Value="6" Text="Comedy"></asp:ListItem>

    </asp:CheckBoxList></td>

    </tr>

    </table>
    </div>
    </form>
    </body>
    </html>
    [/php]

    Scrrenshot Describe the Output

    Any suggestion and question-related to this article please comment me. Have a nice Day.Thank You

    Leave a Reply

    Your email address will not be published.