Simple code for Select Multiple Value from Cheakbox List and save into Database

Posted by Sandeepraturi under C# category on | Points: 40 | Views : 1689

id = int(auto increment,primary key)
chk = varchar(50)

-------------------put this code in body tag--------------------
<form id="form1" runat="server">
<div align="center">
<h1 align="center">Select Name</h1>
<asp:CheckBoxList ID="Chk" runat="server">
<asp:Button ID="btn" runat="server" Text="submit" onclick="btn_Click" /></div>

-------------------------c# code------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace sandeep
public partial class WebForm2 : System.Web.UI.Page
SqlConnection con = new SqlConnection("server=SANDEEP-PC;Database=data;Integrated Security=True");
protected void Page_Load(object sender, EventArgs e)
if (IsPostBack != true)


protected void btn_Click(object sender, EventArgs e)
string str = string.Empty;

for (int k = 0; k < Chk.Items.Count; k++)
if (Chk.Items[k].Selected == true)
str = str + Chk.Items[i].Text + ",";

SqlCommand cmd = new SqlCommand(" insert into chk values ('" +str+ "') " , con);



Comments or Responses

Posted by: T.Saravanan on: 10/28/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Kindly post your code inside the code tag.

Note: Don't assign variable as 'i' in loop because in our DNF square brace of 'i' is consider as "Italic" format. So please change the variable in your code instead of "i".
Posted by: T.Saravanan on: 10/29/2012 Level:Silver | Status: [Member] [MVP] | Points: 10
Yes, You are correct. But in DNF its consider as "Italic" format.

Login to post response