Я создаю метод для обработки события кнопки удаления внутри DataList, и он выполняет функции должным образом, однако я получаю это исключение:
Collection was modified; enumeration operation may not execute.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
, и это мой код:
protected void delete(object sender, CommandEventArgs e)
{
if ((e.CommandName == "delete") && (e.CommandArgument != null))
{
foreach (DataListItem item in DataList2.Items)
{
Label post_IDLabel = (Label)item.FindControl("post_IDLabel");
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("delete_post", conn);
cmd.CommandType = CommandType.StoredProcedure;
int post_ID = Convert.ToInt32(post_IDLabel.Text);
string email = Session["email"].ToString();
int course_ID = Convert.ToInt32(Request.QueryString["courseID"]);
cmd.Parameters.Add(new SqlParameter("@course_ID", course_ID));
cmd.Parameters.Add(new SqlParameter("@myemail", email));
cmd.Parameters.Add(new SqlParameter("@post_ID", post_ID));
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
DataList2.DataBind();
}
}