【问题标题】:InvalidOperationException errorInvalidOperationException 错误
【发布时间】:2012-01-30 04:45:43
【问题描述】:

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

【问题讨论】:

  • 也许将数据绑定移出循环。
  • 您在 foreach 循环中绑定到 DataList - 这导致集合被修改。

标签: c# asp.net visual-studio-2010 exception-handling datalist


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

      • 非常感谢蒂姆,它成功了,但我没有得到关于帖子 ID 的部分?你能解释一下吗? :)
      • 好问题 :-) 你可以在这里找到更多信息:stackoverflow.com/questions/43249/…
      • 想法是你将id作为列表传递给存储过程
      • 我去你的意思,但是我不想删除所有的值,只是选择的项目,你知道怎么做吗?
      • 现在我看到您只想删除单个项目。如果要删除单个项目,则不应执行循环。相反,您应该使用 CommandArgument 来检索 id。所以你应该删除循环,并使用这样的东西来检索 id: int post_ID = (int) e.CommandArgument;
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-30
      • 2016-05-29
      相关资源
      最近更新 更多