我需要將選中的值從複選框列表輸入到數據庫中的多行表中。在數據庫的不同行中插入多選複選框列表值
我的代碼是:
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();
}
}
}
但這代碼不起作用。
但是我沒有收到任何錯誤,但是沒有插入數據。
在代碼頂部設置一個斷點,然後通過代碼來查看問題。 –
問題在於if條件....它沒有進入if塊 – ap2307
在斷點模式下,將光標放在「CheckBoxList1.Items」上並確保提供的值是否合適。 –