Note that there are some explanatory texts on larger screens.

plurals
  1. POInvalidOperationException error
    text
    copied!<p>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:</p> <pre><code>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. </code></pre> <p>and this is my code: </p> <pre><code>protected void delete(object sender, CommandEventArgs e) { if ((e.CommandName == "delete") &amp;&amp; (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(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload