ap2307
ap2307

Reputation: 1

Insert muliple select checkbox list value in different rows of database

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.

Upvotes: 0

Views: 1734

Answers (1)

Grant Winney
Grant Winney

Reputation: 66439

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);
...
...

Upvotes: 2

Related Questions