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.
CheckBoxList1.Itemsand make sure whether the provided values are appropriate first.