How we will save many check box values in one data base column. Many times we need to save value multiple values on one column so we can use this below code
Table Design:-
id int
IdCard varcher
Table Design:-
id int
IdCard varcher
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>
<asp:Button ID="BtnSubmit" runat="server" Text="Submit"
onclick="BtnSubmit_Click" />
</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)
{
}
protected void BtnSubmit_Click(object sender, EventArgs e)
{
String str = "";
for (int i = 0; i <= CBLID.Items.Count - 1; i++)
{
if (CBLID.Items[i].Selected)
{
if (str == "")
{
str = CBLID.Items[i].Text;
}
else
{
str += "," + CBLID.Items[i].Text;
}
}
}
SqlConnection con = new SqlConnection("Data Source=NEERAJ-PC;Initial Catalog=CodeSolution;Persist Security Info=True;User ID=sa; password=12345678");
con.Open();
SqlCommand cmd = new SqlCommand("Insert into reg(IDcard) values('" + str + "')", con);
cmd.ExecuteNonQuery();
Response.Write("Your data Sucessfully saved");
CBLID.SelectedIndex=-1;
}
}
0 comments:
Post a Comment