Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
1.80/5 (3 votes)
See more:
C# .NET - how to save multiple checkbox values in single column in database
Posted
Updated 28-Jun-16 7:33am

C#
string strCheckValue = "";

        if (chk1.Checked)

        {

          strCheckValue = strCheckValue + "," + chk1.Text;

        }

        if (chk2.Checked)

        {

          strCheckValue = strCheckValue + "," + chk2.Text;

        }

        strCheckValue = strCheckValue.TrimStart(',');

Now store strCheckValue to Database.

or----

try this link

How to insert the values from each checked checkboxes into the database
 
Share this answer
 
v3
Comments
Member 12341674 23-Feb-16 8:04am    
how to store multiple check box values which is assigned to one table and stored into another table.
Hi,

Create a class like this:

C#
[Serializable]
public class CheckBoxValue
{
    [XmlElement("Value")]
    public bool Value {get; set;}
    
    [XmlElement("CheckBoxName")]
    public string CheckBoxName{get; set;}
}



Then you can serialize it as Xml then insert it into Database.

Or use this solution:
multiple input from users to save in database in one column
 
Share this answer
 
v2
cbkitems is CheckBoxList controlID

C#
if (cbkitems.SelectedIndex >= 0)
               {
                   string state1 = "";
                   foreach (ListItem s in cbkitems.Items)
                   {
                       if (s.Selected)
                       {

                           state1 += s + ",";

                       }
                   }
                   state1 = state1.Substring(0, state1.Length - 1);

                   DB_ColumnName = state1;



               }
               else
               {
                     DB_ColumnName  = "";
               }
       }
 
Share this answer
 
v3
C#
foreach (ListItem s in checkbox.Items)
{
if (s.Selected)
{

string+= s + ",";// your final output will be in this one try this

}
 
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