Loop fires insert statement twice

bethkomodo

New Member
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??\[code\]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();}\[/code\]
 
Back
Top