How to fetch database values in checkboxlist in asp.net 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 asp.net c#
Database:-
Source Code:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chkboxlist.aspx.cs" Inherits="chkboxlist" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<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>Passport</asp:ListItem>
<asp:ListItem>Student ID</asp:ListItem>
<asp:ListItem>Voter
ID</asp:ListItem>
<asp:ListItem>Aadhar
Card</asp:ListItem>
</asp:CheckBoxList>
</fieldset>
</div>
</form>
</body>
</html>
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)
{
showdata();
}
}
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];
con.Open();
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;
break;
}
}
}
con.Close();
}
}
Out-Put:-
How to fetch database values in checkboxlist in asp.net c#
Reviewed by NEERAJ SRIVASTAVA
on
5:36:00 PM
Rating:
sir this insert all values but i want those value inserted which we select.......Plz sir issue this probleb
ReplyDelete