I have a a loop that iterates through the selections in an checkboxlist. The problem is the loop fires the insert statement twice for each selection. So if the user checks one box, it inserts 2 rows. If the user selects 3 boxes, it inserts 6 rows and so on. How can I make sure it only fires the insert once per selection??
protected void btn_test_Click(object sender, EventArgs e) {
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Insert into t_ap_line_setup (line,date) values (@line,getdate())";
cmd.Connection = this.sqlConnection1;
this.sqlConnection1.Open();
foreach ( ListItem li in lines_list.Items ) {
if (li.Selected == true) {
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@line", li.Text);
cmd.ExecuteNonQuery();
}
}
this.sqlConnection1.Close();
}
Doesn’t look like the loop is running twice. I suspect your click handler is firing twice due to duplicate event subscriptions.