locked
Enumeration Error on SQL insert from Gridview RRS feed

  • Question

  • User1215529056 posted

    I have this in the code behind file. When I run it on the site I get :

    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.

    Source Error:

    Line 11: 
    Line 12:     {
    Line 13:         foreach (System.Collections.DictionaryEntry entry in e.Values)
    Line 14:         {
    Line 15:             e.Values[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());

    Any suggestions?

    Code behind file:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class VendorAdd : System.Web.UI.Page
    {
    public void dvAddVendor_ItemInserting(Object sender, DetailsViewInsertEventArgs e)

    {
    foreach (System.Collections.DictionaryEntry entry in e.Values)
    {
    e.Values[entry.Key] = System.Web.HttpUtility.HtmlEncode(entry.Value.ToString());
    }
    }

    protected void dvAddVendor_ItemCommand(object sender, DetailsViewCommandEventArgs e)
    {
    if (e.CommandName == "Cancel")
    {
    Response.Redirect("~/VendorsShow.aspx");


    }
    }
    }

    It used to work and I am not sure what changed but I need it to work.

    Tuesday, May 14, 2019 5:50 PM

All replies

  • User-893317190 posted

    Hi RuthlessRoth,

    It seems that you modify the collection when you loop through the collection.

    I suggest that you could use for i loop instead of for each loop.

    Below is my code.

     <asp:DetailsView ID="DetailsView1"  AllowPaging="true" OnItemInserting="DetailsView1_ItemInserting" DefaultMode ="Insert"  OnItemUpdating="DetailsView1_ItemUpdating" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataKeyNames="id" DataSourceID="SqlDataSource1"  ValidateRequestMode="Disabled" >
    <Fields>
    <asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" />
    <asp:BoundField DataField="Info" HeaderText="Info" SortExpression="Info" />

    <asp:BoundField DataField="catname" HeaderText="catname" SortExpression="catname" />
    <asp:BoundField DataField="desc" HeaderText="desc" SortExpression="desc" />
    <asp:CommandField ShowInsertButton="true" />
    </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EntityExeConnectionString %>" DeleteCommand="DELETE FROM [First] WHERE [id] = @id" InsertCommand="INSERT INTO [First] ([Info], [catname], [desc]) VALUES (@Info,@catname, @desc)" SelectCommand="SELECT * FROM [First]" UpdateCommand="UPDATE [First] SET [Info] = @Info, [catname] = @catname, [desc] = @desc WHERE [id] = @id">
    <DeleteParameters>
    <asp:Parameter Name="id" Type="Int32" />
    </DeleteParameters>
    <InsertParameters>
    <asp:Parameter Name="Info" Type="String" />

    <asp:Parameter Name="catname" Type="String" />
    <asp:Parameter Name="desc" Type="String" />
    </InsertParameters>
    <UpdateParameters>
    <asp:Parameter Name="Info" Type="String" />

    <asp:Parameter Name="catname" Type="String" />
    <asp:Parameter Name="desc" Type="String" />
    <asp:Parameter Name="id" Type="Int32" />
    </UpdateParameters>
    </asp:SqlDataSource>

    Code behind.

       protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {

    for (int i = 0; i < e.Values.Count; i++)
    {
    e.Values[i] = System.Web.HttpUtility.HtmlEncode(e.Values[i]);
    }
    DetailsView1.DefaultMode = DetailsViewMode.ReadOnly;

    }

    The result.

    Best regards,

    Ackerly Xu

    Wednesday, May 15, 2019 6:44 AM