无效操作异常错误
我正在创建一个方法来处理 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();
}
}
I am creating a method to handle delete button event inside a DataList, and it's doing the functionality properly, however I get this exception:
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.
and this is my code:
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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
将
DataList2.DataBind();
从foreach
循环中取出Take
DataList2.DataBind();
out of theforeach
loop在枚举集合时,您不能修改集合。
DataList2.DataBind
修改DataList2.Items
,这是不允许的。如果将
DataBind
移到循环之外,它应该可以工作。You may not modify the collection while you are enumerating it.
DataList2.DataBind
modifiesDataList2.Items
, which is not allowed.If you move
DataBind
outside the loop, it should work.我想当您将 DataList2.DataBind 移出 foreach 时,您的问题就解决了。
但我认为你的代码有更多错误,你应该尽量避免从循环中调用数据库。您应该尝试重构此代码,以便只对数据库进行一次调用。例如,将所有帖子 ID 传递到一个参数中。或者也许只有 course_id 和电子邮件(如果足够的话)。
I guess your problem is solved when you move the DataList2.DataBind out of the foreach.
But I think there is more wrong with your code, you should try to avoid calling the database from a loop. You should try to refactor this code so that you only make one call to the database. For example pass all post ID's in one parameter. Or maybe only the course_id and email if it is sufficient.