Sunday 2 February 2014

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


Database

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


Put this code in body tag 

<body> 
<form id="form1" runat="server"> 
<div align="center"> 
<h1 align="center">Select Name</h1> 
<asp:CheckBoxList ID="Chk" runat="server"> 
<asp:ListItem>Sandeep</asp:ListItem> 
<asp:ListItem>Dinesh</asp:ListItem> 
<asp:ListItem>Pradeep</asp:ListItem> 
<asp:ListItem>Kuldeep</asp:ListItem> 
</asp:CheckBoxList> 
<asp:Button ID="btn" runat="server" Text="submit" onclick="btn_Click" /></div> 
</form> 
</body> 

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 + ","; 



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






}

No comments:

Post a Comment