Click here to Skip to main content
16,018,650 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to store multiple value of checkboxlist

here is my checkbox list

XML
<asp:CheckBoxList ID="chkMaritalStatus" runat="server" CssClass="rb" RepeatDirection="Horizontal">
<asp:ListItem>Unmarried</asp:ListItem>
<asp:ListItem>Widow</asp:ListItem>
<asp:ListItem>Divorced</asp:ListItem>
<asp:ListItem>Awaiting divorce</asp:ListItem>
<asp:ListItem>Any</asp:ListItem>
</asp:CheckBoxList>


and i want to multiple items in my database how can i do that?
Posted
Updated 10-Jul-12 19:30pm
v2
Comments
vangapally Naveen Kumar 11-Jul-12 1:15am    
Can You elaborate more.....

Assuming that your database contains single column called MaritalStatus which can accept comma separated values, see if below code helps you:

C#
string status= string.Empty; 
        foreach (ListItem item in this.CheckBoxList1.Items) 
            if (item.Selected) 
                status += item + ","; 
 
        SqlCommand cmd = new SqlCommand("Insert into Demo values('" + status+ "')", cn); //Write Your Query to insert into database table        
        cn.Open(); 
        int iCount = cmd.ExecuteNonQuery(); 
        cn.Close();
 
Share this answer
 
C#
Here,chklstSubject is checkbox name and we are store selected items in sub.

          String sub="";
          for (int i = 0; i < chklstSubject.Items.Count; i++)
            {
                if (chklstSubject.Items[i].Selected)
                {
                    sub += chklstSubject.Items[i].Value + ",";
                }
            }
            sub = sub.TrimEnd(',');


insert sub into query,

String qr="insert into tabl1 values('"+sub+"')";
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900