Click here to Skip to main content
13,250,883 members (48,186 online)
Rate this:
Please Sign up or sign in to vote.
See more:

I was not able to insert the selected values of a checkbox list into the table.
Could you please help me out..

Thanks in advance!

The code I tried is:

public void btnClick()
           for (int i = 0; i < 4; i++)
               CheckBoxList2.SelectedIndex = i;
               if (CheckBoxList2.Items[i].Selected)
                   SqlCommand cmd1 = new SqlCommand("insert into DART_IDC_ADD_PROC_MAP(DAR_ID,ADD_PROC_SEL) values (123,'" + CheckBoxList2.Items[i].Text.ToString() + "'", con1);
               MessageBox.Show("Inserted "+CheckBoxList2.SelectedValue.ToString());
           da = new SqlDataAdapter("insert into DART_IDC_ADD_PROC_MAP values (123,"+i.ToString(), con1);
Posted 22-Dec-12 0:22am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

And the issue is?

BTW, pretty strange, you have few line of code that does not go what you say:
1. CheckBoxList2.SelectedIndex = i; (Why?)
2. MessageBox.Show (in ASP.NET?)

You have not written a good code. For what you seek, you need to do:
1. Loop though all the items of the checkboxlist
2. See if the current item is selected or not
3. If selected, put an insert statement for the item
4. Move to next item.

Simple! Try out.
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Check this
string qry;
foreach (ListItem item in CheckBoxList2.Items)
    if (item.Selected)
       qry = "INSERT INTO DART_IDC_ADD_PROC_MAP(DAR_ID,ADD_PROC_SEL) VALUES (123,'" + item.Text + "')";
       SqlCommand cmd = new SqlCommand(qry, con1);

I hope conection string is ok and "DAR_ID" is not primary or unique and "ADD_PROC_SEL" is of varchar/nvarchar or char/char

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100