Results 1 to 3 of 3

Thread: Error Msg : Collection was modified: enumeration operation may not execute

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    101

    Question Error Msg : Collection was modified: enumeration operation may not execute

    Good day.

    I have a datagrid which supposed to receive data input from user and when the user click on Save button, i use the function as below, to iterate throw each datarow and manually append it into db.

    For Each objDR In tblUserGroupAdd.Rows
    objUserGroup = New My.BusinessFacade.UserGroup

    Select Case objDR.RowState
    Case DataRowState.Added
    objUserGroup.append(objDR("description", DataRowVersion.Default), objDR("remarks", DataRowVersion.Default), Nothing)
    tblUserGroupAdd.RemoveTbl_User_GroupRow(objDR)

    Case Else

    End Select
    Next

    Upon success, teh datarow will be removed or else, it will remain in the datagrid.

    But, when I try to exec the program, it return the error message "Collection was modified: enumeration operation may not execute".

    What happen ? Is there any better way to code the same thing instead of the function I am using now ?

    Thanks.

    SonicWave

  2. #2

    Thread Starter
    Lively Member
    Join Date
    Jan 2002
    Posts
    101
    Good day.

    I found an explaination in http://www.dotnet247.com/247referenc...31/158752.aspx saying that it's a problem with the looping.

    But in another datagrid for the user to add, update adn delete existing data using the function below, it works flawlessly. I not really understand what happen.

    For Each objDR In tblUserGroupAmmend.Rows
    objUserGroup = New My.BusinessFacade.UserGroup

    Select Case objDR.RowState
    Case DataRowState.Added
    objUserGroup.append(objDR("description", DataRowVersion.Proposed), objDR("remarks", DataRowVersion.Proposed), Nothing)

    Case DataRowState.Deleted
    objUserGroup.delete(objDR("id", DataRowVersion.Original), Nothing)
    tblUserGroupAmmend.RemoveTbl_User_GroupRow(objDR)

    Case DataRowState.Modified
    objUserGroup.update(objDR("id"), objDR("description", DataRowVersion.Proposed), objDR("remarks", DataRowVersion.Proposed), Nothing)

    Case Else

    End Select
    Next

    Help.

    SonicWave

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    When you affect the enumeration when using a foreach loop, things get messed up.

    In order to modify contents in the way you are doing, you need to use a for loop, and loop in reverse. This won't mess with the elements that you are looping through.

    The basic reason is you are modifying the elements you are looping through. When you do this, the iterator no longer knows what is next.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width