无效操作异常错误

发布于 2024-12-23 12:34:13 字数 1792 浏览 1 评论 0原文

我正在创建一个方法来处理 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无声无音无过去 2024-12-30 12:34:13

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

Take DataList2.DataBind(); out of the foreach loop

            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();
一口甜 2024-12-30 12:34:13

在枚举集合时,您不能修改集合。 DataList2.DataBind 修改 DataList2.Items,这是不允许的。

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

You may not modify the collection while you are enumerating it. DataList2.DataBind modifies DataList2.Items, which is not allowed.

If you move DataBind outside the loop, it should work.

长途伴 2024-12-30 12:34:13

我想当您将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文