0

I need to enter selected values from a checkbox list into multiple rows of a table in my database.

My code is:

SqlConnection con1 = new SqlConnection(cs);

string com1 = "INSERT INTO TVC_booking (TVC_booking_lId ,TVC_booking_date, TVC_booking_start_time,TVC_booking_end_time , TVC_booking_subject ,TVC_booking_description,TVC_booking_bookingId,TVC_booking_chairperson, TVC_booking_client,TVC_booking_currentdt,TVC_booking_status)"
              + "VALUES (@lid, @dt, @stime, @etime, @sub, @dsc, @bid, @cp, @cl, @cdt, @stat)";

for (int i = 0; i < CheckBoxList1.Items.Count; i++)
{
    if (CheckBoxList1.Items[i].Selected == true)
    {
        using (SqlCommand cmd1 = new SqlCommand(com1, con1))
        {
            cmd1.Parameters.AddWithValue("lid", CheckBoxList1.Items[i].ToString());
            cmd1.Parameters.AddWithValue("dt",field_date);
            cmd1.Parameters.AddWithValue("stime", field_stime);
            cmd1.Parameters.AddWithValue("etime", endtime);
            cmd1.Parameters.AddWithValue("sub", TextBox_sub.Text);
            cmd1.Parameters.AddWithValue("dsc", TextBox_des.Text);
            cmd1.Parameters.AddWithValue("bid", str);
            cmd1.Parameters.AddWithValue("cp", TextBox_cp.Text);
            cmd1.Parameters.AddWithValue("cl", TextBox_client.Text);
            cmd1.Parameters.AddWithValue("cdt", DateTime.Now);
            cmd1.Parameters.AddWithValue("stat", 1);

            con1.Open();
            cmd1.ExecuteNonQuery();
            con1.Close();
        }
    }
}

But this code does not work.

I don't get any error however data is not inserted.

3
  • Set a break point on top of your code and follow that through the code to see the problem. Commented Jun 23, 2014 at 3:34
  • The issue is with the if condition.... its is not entering the if block Commented Jun 23, 2014 at 3:59
  • On the break-point mode hold the cursor over the CheckBoxList1.Items and make sure whether the provided values are appropriate first. Commented Jun 23, 2014 at 4:27

1 Answer 1

2

Every time I've used AddWithValue(), I've had to include the @ symbol in the name. I'd think omitting it would cause some sort of exception, but maybe it doesn't.. or the exception is being swallowed.

cmd1.Parameters.AddWithValue("@lid", CheckBoxList1.Items[i]);
cmd1.Parameters.AddWithValue("@dt", field_date);
cmd1.Parameters.AddWithValue("@stime", field_stime);
cmd1.Parameters.AddWithValue("@etime", endtime);
...
...
Sign up to request clarification or add additional context in comments.

1 Comment

The problem was solved.... I was making some mistakes in the previous code...This code works fine.... Thank You

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.