Welcome to WebmasterWorld Guest from

Forum Moderators: ocean10000

Message Too Old, No Replies

Inserting checkbox values into one field

8:23 am on May 8, 2009 (gmt 0)

New User

10+ Year Member

joined:May 8, 2009
votes: 0


I have a checkboxes on my page (.aspx) like

checkbox1 - item1
checkbox2 - item2
checkbox3 - item3

what i want is that when a user selects certain checkboxes
they should be inserted into a table called
customer which has fields (customer id and actions)

the data should be inserted as

customerid actions
1 item1,item2,item3
2 item1,item3
3 etc..

i.e all the values should be inserted into database into one field separated by commas.


[edited by: coopster at 2:26 pm (utc) on May 8, 2009]
[edit reason] no signatures please TOS [webmasterworld.com] [/edit]

4:26 pm on May 8, 2009 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Feb 1, 2005
votes: 0

If you have a set number of Checkboxes you could do the following:

StringBuilder sb = new StringBuilder();

if (item1.checked) {sb.Append("item1,")}
if (item2.checked) {sb.Append("item2,")}
if (item3.checked) {sb.Append("item3,")}

string sql = "INSERT INTO Customer (actions) VALUES (" + sb.ToString() + ")"

But, if you are dynamically creating the checkboxes you will need to loop through them.

StringBuilder sb = new StringBuilder();
foreach (Control myControl in form1.Controls)
if (myControl is CheckBox)
CheckBox tempCheckBox;
tempCheckBox = (CheckBox)myControl;
if (tempCheckBox.Checked)
sb.Append(tempCheckBox.ID + ",")

I havent tested this, but you should get the general idea

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members