Solved

Error: Collection was modified; enumeration operation may not execute.

Posted on 2005-10-05
5
14,731 Views
Last Modified: 2011-08-18
Hi There,

I'm getting an error when I try to update multiple rows from shopping cart. When I have one row it works fine but when I have multiple rows it gives me the error "Collection was modified; enumeration operation may not execute. " after the first iteration of the loop.

Could someone help me out with the syntax please?

Sean

!--- code

 Sub ChangeQuantity(ByVal send As Object, ByVal E As EventArgs)
        Dim i As Int32 = 0
        Dim strCode As String = ""
        Dim intusercode As Int32 = 0
        ' For i = 0 To cart.Items.Count - 1
        For Each dataItem As RepeaterItem In cart.Items
            Dim ItemQuantity As TextBox = (CType(dataItem.FindControl("ItemQuantity"), TextBox))
            Dim txtcode As TextBox = (CType(dataItem.FindControl("txtcode"), TextBox))
            Dim txtitemnumber As Label = (CType(dataItem.FindControl("lblPitemnumber"), Label))
            Dim lblprice As Label = (CType(dataItem.FindControl("lblprice"), Label))
            If Not txtcode.Text = "" Then
                strCode = GetValidCode(txtcode)
                If strCode Is Nothing Then 'if the user does not have a valid code then call the function
                    Call UpdateCart(ItemQuantity, txtcode, txtitemnumber, lblprice, intusercode)
                Else 'if the user has a valid code then look it up
                    Session("THECODE") = strCode
                    intusercode = 1
                    Call GetDistributorPrice(ItemQuantity, txtcode, txtitemnumber, lblprice, intusercode)
                End If
            Else
                'if the code is nothing then call the function
                Call UpdateCart(ItemQuantity, txtcode, txtitemnumber, lblprice, intusercode)
            End If
            'Exit For
        Next
    End Sub


!--- repeater code

<asp:repeater id="cart" runat="server">
                        <HeaderTemplate>
                              <table border="1" cellpadding="3" style="background-color:#F6F6F6; border-collapse:collapse">
                                    <tr>
                                          <th>
                                                Number</th>
                                          <th>
                                                Title</th>
                                          <th>
                                                Price</th>
                                          <th>
                                                Quantity</th>
                                          <th>
                                                MTA / Reseller Code</th>
                                          <th>
                                                Amount</th>
                                          <th>
                                                &nbsp;
                                          </th>
                                    </tr>
                        </HeaderTemplate>
                        <ItemTemplate>
                              <tr>
                                    <td align="center">Item Number:
                                          <%# Container.DataItem("Pitemnumber") %>
                                          <asp:Label id="lblPitemnumber" runat="server" ReadOnly="True" Visible="false" Text='<%# Container.DataItem("Pitemnumber") %>'>
                                          </asp:Label>
                                    </td>
                                    <td align="left">Item Name:
                                          <asp:Label id="lblPitemname" runat="server" ReadOnly="True" Visible="true" Text='<%# Container.DataItem("Pname") %>'>
                                          </asp:Label>
                                    </td>
                                    <td align="right">Item Price:
                                          <asp:Label id="lblprice" runat="server" ReadOnly="True" Visible="true" Text='<%# Container.DataItem("Pprice") %>'>
                                          </asp:Label>
                                          <asp:Label id="lblPhandling" runat="server" ReadOnly="True" Visible="False" Text='<%# Container.DataItem("Phandling") %>'>
                                          </asp:Label>
                                    </td>
                                    <td align="center">
                                          <asp:TextBox id="ItemQuantity" runat="server" Text='<%# Container.DataItem("Pitemquantity") %>' MaxLength="2" Style="width:30px; text-align:right"/>
                                    </td>
                                    <td>
                                          <asp:TextBox id="txtcode" runat="server" MaxLength="11" Style="width:100px; text-align:right" /></td>
                                    <td align="right"><b><%# FormatNumber(GetItemAmount(Container.DataItem("Pprice"), Container.DataItem("pitemquantity")), 2) %></b></td>
                        </ItemTemplate>
                        <FooterTemplate>
                              <tr>
                                    <th align="right" colspan="4">
                                          Postage & Handling:
                                    </th>
                                    <td align="right"><b><%# FormatCurrency(GetOrderShipping()) %></b></td>
                              </tr>
                              <tr>
                                    <th align="right" colspan="4">
                                          Order Total:
                                    </th>
                                    <td align="right"><b><%# FormatCurrency(GetOrderTotal()) %></b></td>
                                    <td>
                                          <asp:Button Text="Update" class="small" runat="server" onclick="ChangeQuantity" ID="Button1"
                                                NAME="Button2" />
                                    </td>
                              </tr>
                        </FooterTemplate>
                  </asp:repeater>
0
Comment
Question by:bullrout
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 15028265
I think i...after excuting the UpdateCart() function ... it goes to page load............
or/and updated data must be bind again with repeater control anywhere else in your code........
make sure what happens exactly.

ThanX :)
0
 
LVL 21

Accepted Solution

by:
tovvenki earned 250 total points
ID: 15028405
Hi,
from the help on For...each
"The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects."

instead of using For..each iterate using an index like

for index = 0 to cart.items.count
...
next

hope that this helps you

Regards
Venki
0
 
LVL 5

Author Comment

by:bullrout
ID: 15028505
Hi tovvenki,

I have changed this using your suggestion but now when I try to pick up the contents of the textboxes & labels and I get an error saying "Object reference not set to an instance of an object. " How can I access the values of the objects?
It falls over on this line "If Not txtcode.Text = "" Then"


Sub ChangeQuantity(ByVal send As Object, ByVal E As EventArgs)
        Dim i As Int32 = 0
        Dim strCode As String = ""
        Dim intusercode As Int32 = 0
        For i = 0 To cart.Items.Count - 1
            '  For Each dataItem As RepeaterItem In cart.Items
            Dim ItemQuantity As TextBox = (CType(cart.FindControl("ItemQuantity"), TextBox))
            Dim txtcode As TextBox = (CType(cart.FindControl("txtcode"), TextBox))
            Dim txtitemnumber As Label = (CType(cart.FindControl("lblPitemnumber"), Label))
            Dim lblprice As Label = (CType(cart.FindControl("lblprice"), Label))

            If Not txtcode.Text = "" Then
                strCode = GetValidCode(txtcode)
                If strCode Is Nothing Then 'if the user does not have a valid code then call the function
                    Call UpdateCart(ItemQuantity, txtcode, txtitemnumber, lblprice, intusercode)
                Else 'if the user has a valid code then look it up
                    Session("THECODE") = strCode
                    intusercode = 1
                    Call GetDistributorPrice(ItemQuantity, txtcode, txtitemnumber, lblprice, intusercode)
                End If
            Else
                'if the code is nothing then call the function
                Call UpdateCart(ItemQuantity, txtcode, txtitemnumber, lblprice, intusercode)
            End If
            'Exit For
        Next
    End Sub
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 250 total points
ID: 15028541
   For i = 0 To cart.Items.Count - 1
            '  For Each dataItem As RepeaterItem In cart.Items
            Dim dataitem as RepeaterItem  =  cart.Items(i)
                   
            Dim ItemQuantity As TextBox = (CType(dataitem.FindControl("ItemQuantity"), TextBox))

         
0
 
LVL 5

Author Comment

by:bullrout
ID: 15028610
Yah thanks guys!!!!
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question