Click here to Skip to main content
12,621,155 members (34,238 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0 ASP.NET4
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:

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 22-Dec-12 0:22am
arevant267
Rate this: bad
 
good
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.
  Permalink  
Rate this: bad
 
good
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);
       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
  Permalink  

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 | Mobile
Web02 | 2.8.161128.1 | Last Updated 22 Dec 2012
Copyright © CodeProject, 1999-2016
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