0

I'm using ASP.net and i've added a delete button on a gridview and im trying to remove the row of the button clicked. The thing is though, that i get an InvalidOperationException on my foreach.

I may have other errors in the code or my logic may be wrong so if you see any of it please point them out. Thanks.

gridView cart.ASPX:

<asp:GridView ID="CartGrid" runat="server" AutoGenerateColumns="false" OnRowDeleting="RemoveSelected">
    <Columns>
        <asp:BoundField DataField="product_name" HeaderText="Name" />
        <asp:BoundField DataField="price_per_unit" HeaderText="Price" />
        <asp:BoundField DataField="unit" HeaderText="Unit" />

        <asp:TemplateField>
        <ItemTemplate>
        <asp:LinkButton ID="delSelected" runat="server" Text="Delete" CommandName="Delete"></asp:LinkButton>

        </ItemTemplate>
        <ItemStyle Width="100px" />

        </asp:TemplateField>
    </Columns>
</asp:GridView>

delete method cart.ASPX.CS

protected void RemoveBtn(object sender, GridViewDeleteEventArgs e)
    {
        ArrayList cartList = (ArrayList)Session["cartList"];

        GridViewRow row = (GridViewRow)CartGrid.Rows[e.RowIndex];

        String name = row.Cells[0].Text;
        //String name = (string)CartGrid.DataKeys[e.RowIndex].Value;
        //String name = CartGrid.SelectedRow.Cells[1].Text;

        foreach (product p in cartList)
        {
            if (p.product_name.Equals(name))
            {
                cartList.Remove(p);
            }
        }

        CartGrid.DataBind();
    }
Albin Åberg
  • 39
  • 1
  • 8
  • You cant modify an iteration variable or rather the collection that you are iterating on. – C4d Feb 27 '17 at 12:59
  • Possible duplicate of [Efficiently deleting item from within 'foreach'](http://stackoverflow.com/questions/8791557/efficiently-deleting-item-from-within-foreach) – klashar Feb 27 '17 at 13:00
  • Possible duplicate of [Why can't I modify the loop variable in a foreach?](http://stackoverflow.com/questions/3551696/why-cant-i-modify-the-loop-variable-in-a-foreach) – C4d Feb 27 '17 at 13:00

2 Answers2

2

Hi I think the issue is that you are using the cartlist for loop and at the same time you want to delete values from the same which is not possible .

just you need to change your approach like create the empty list and add all the index into it from where you have to delete the value Staring from bottom to top like from end of list so that it can not effect the index of the list by deleted value and after loop you can delete it using the new empty list which contain the list of all the values which should be deleted.

Anurag_Soni
  • 542
  • 2
  • 17
  • Thanks! That solved my problem, my hero without cape! Ill accept the answer in 1 minute. Your comment made me understand the situation alot better. I made a temp product that gets the value of the p product when the if statement in the foreach is fulfilled instead. – Albin Åberg Feb 27 '17 at 13:10
  • Thanks @albinaberg – Anurag_Soni Feb 27 '17 at 13:12
1

You can't modify the collection you're iterating over with the foreach - please see this question for more information.

Community
  • 1
  • 1
DrMistry
  • 321
  • 1
  • 13