首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >InvalidOperationException错误

InvalidOperationException错误
EN

Stack Overflow用户
提问于 2011-12-30 05:01:08
回答 3查看 830关注 0票数 4

我正在创建一个方法来处理DataList中的delete按钮事件,它正确地执行了该功能,但是我得到了这个异常:

代码语言:javascript
复制
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.

这是我的代码:

代码语言:javascript
复制
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();
                }
            }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-12-30 05:04:45

DataList2.DataBind();foreach循环中删除

代码语言:javascript
复制
            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();
票数 2
EN

Stack Overflow用户

发布于 2011-12-30 05:05:32

枚举集合时不能对其进行修改。DataList2.DataBind修改DataList2.Items,这是不允许的。

如果您将DataBind移到循环之外,它应该可以工作。

票数 2
EN

Stack Overflow用户

发布于 2011-12-30 05:08:51

我猜当您将DataList2.DataBind移出foreach时,您的问题就解决了。

但我认为你的代码有更多的错误,你应该尽量避免从循环调用数据库。您应该尝试重构此代码,以便只对数据库进行一次调用。例如,在一个参数中传递所有的帖子ID。或者,如果足够的话,可能只有course_id和电子邮件。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8673399

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档