Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert multiple checkbox list values in my SQL database in single column. Only one value is inserting because of insert statement.
I want multiple values to be inserted. For-loop is not working.
Any help would be appreciated.
C#
for (int i = 0; i < CheckBoxList1.Items.Count - 1; i++)
        {
            String str = "";

            if (CheckBoxList1.Items[i].Selected)
            {

                str = CheckBoxList1.Items[i].Text;

                con.Open();

                string sql = "Insert into LibManAddBook(Category,BookTitle,Feature,SubCategory)values('" + DDLCategory.SelectedItem.Text + "','" + TxtBooktitle.Text + "','" + CheckBoxList1.Text + "','" + DDLSubcategory.SelectedItem.Text + "')";

                SqlCommand cmd = new SqlCommand(sql, con);
....
            }
}
Posted
Updated 2-Aug-10 20:02pm
v2
Comments
nagendrathecoder 3-Aug-10 1:58am    
Where are you inserting "str" in the query?
What you need to insert?

If you would had debugged then you would had found that execution does enter in For-Loop. Your Insert query is not correct.

You need to use: CheckBoxList1.Items[i].Text

Try:
string sql = "Insert into LibManAddBook(Category,BookTitle,Feature,SubCategory)values('" + DDLCategory.SelectedItem.Text + "','" + TxtBooktitle.Text + "','" + CheckBoxList1.Items[i].Text + "','" + DDLSubcategory.SelectedItem.Text + "')";


Just use DEBUGGER and see how things are working and you should be able to resolve such issues easily.
 
Share this answer
 
Comments
Sandeep Mewara 2-Oct-12 2:27am    
What VB? Question is in C#

If you have any other question, post a new one.
I did the same what u have said..but its not working..
Any help would be appreciated.
 
Share this answer
 
Comments
nagendrathecoder 3-Aug-10 3:04am    
Have you tried to debug it? How many times your for loop is executing?
Also, please don't post your comment as answer, post it as a comment.

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