Множественный выбор и удаление по gridview

когда я проверяю флажок, он может зацикливаться и получить идентификатор пользователя и productname, но почему бы не позволить мне удалить?

foreach (GridViewRow row in GridView1.Rows)
 {
 var check = row.FindControl("chkdelete") as CheckBox;
 if (check.Checked)
 {
 string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 SqlConnection con = new SqlConnection(connStr);
 var uid = row.FindControl("lbluserid") as Label;
 var pid = row.FindControl("lblproductname") as Label;
 SqlCommand cmd = new SqlCommand("delete from ProductReview where userID=@id and productid=@pid", con);
 cmd.Parameters.AddWithValue("@id", uid.Text);
 cmd.Parameters.AddWithValue("@pid", pid.Text);
 con.Open();
 cmd.ExecuteNonQuery();
 con.Close();
 }

 }
1 ответ

вы можете попробовать одно и то же.

foreach (GridViewRow row in GridView1.Rows)
 {
 var check = row.FindControl("chkdelete") as CheckBox;
 if (check.Checked)
 { 
 var uid = row.FindControl("lbluserid") as Label;
 var pid = row.FindControl("lblproductname") as Label;

 DatabaseHelperClass.DoDatabaseOperation(uid.Text.Trim(), pid.Text.Trim());

 }

}


Class DatabaseHelperClass
{
 Public static void DoDatabaseOperation(string uid , string pid)
 { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
 SqlConnection con = new SqlConnection(connStr);
 SqlCommand cmd = new SqlCommand("delete from ProductReview where userID=@id and productid=@pid", con);
 cmd.Parameters.AddWithValue("@id", uid);
 cmd.Parameters.AddWithValue("@pid", pid);
 con.Open();
 cmd.ExecuteNonQuery();
 con.Close();

 }
}

licensed under cc by-sa 3.0 with attribution.