InvalidOperationException error

本文关键字:error InvalidOperationException | 更新日期: 2023-09-27 18:19:13

我正在创建一个方法来处理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();
                }
            }

InvalidOperationException error

DataList2.DataBind();foreach循环中取出

            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();

在枚举集合时不能修改它。DataList2.DataBind修改了DataList2.Items,不允许修改

如果你把DataBind移出循环,它应该可以工作。

我想你的问题是解决当你移动DataList2。

但是我认为你的代码有更多的错误,你应该尽量避免从循环调用数据库。您应该尝试重构这段代码,以便只对数据库进行一次调用。例如,在一个参数中传递所有的post ID。如果足够的话,也可以只输入course_id和email。