Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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:

C#
public void btnClick()
       {
           con1.Open();
           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);
                   cmd1.ExecuteNonQuery();
               }
               i++;
               MessageBox.Show("Inserted "+CheckBoxList2.SelectedValue.ToString());
           }
           da = new SqlDataAdapter("insert into DART_IDC_ADD_PROC_MAP values (123,"+i.ToString(), con1);
       }
Posted

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.
 
Share this answer
 
Check this
C#
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);
       con1.Open();
       cmd.ExecuteNonQuery();
       con1.Close();
     }
}


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
 
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