How to fetch database values in checkboxlist in c#

As our previous article, we learn about how to save multiple values using checkbox list but now we learn how to fetch that values means How to fetch database values in checkbox list in c#

Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chkboxlist.aspx.cs" Inherits="chkboxlist" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
        <fieldset style="width: 150px">
            <legend>Do you have </legend>

            <asp:CheckBoxList ID="CBLID" runat="server" AutoPostBack="True">
                <asp:ListItem>PAN Card</asp:ListItem>

                <asp:ListItem>Student ID</asp:ListItem>
                <asp:ListItem>Voter ID</asp:ListItem>
                <asp:ListItem>Aadhar Card</asp:ListItem>

Code behind(c#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class chkboxlist : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
        if (!IsPostBack)
    public void showdata()

        SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");

        string[] sstr = new string[100];
        SqlCommand cmd = new SqlCommand("Select * from reg where Id ='" + 3 + "'", con);

        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
            sstr = dr["IDcard"].ToString().Split(',');

        int length = sstr.Length;
        for (int i = 0; i <= sstr.Length - 1; i++)

            string cntry = sstr[i];
            for (int j = 0; j <= CBLID.Items.Count - 1; j++)

                if (CBLID.Items[j].Value == sstr[i])
                    CBLID.Items[j].Selected = true;




How to fetch database values in checkboxlist in c# How to fetch database values in checkboxlist in c# Reviewed by NEERAJ SRIVASTAVA on 5:36:00 PM Rating: 5

1 comment:

  1. sir this insert all values but i want those value inserted which we select.......Plz sir issue this probleb


Powered by Blogger.