Collection was modified; enumeration operation may not execute.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SaradaV
    New Member
    • Aug 2010
    • 1

    #1

    Collection was modified; enumeration operation may not execute.

    Hi all,

    I am working on vb.net web application and Often getting the following error. But even with the same inputs it is not reproducable. In most of the cases its executing successfully and in some rare (unknown) cases only its failing. The interesting point is; I just noted one set of the inputs caused the problem and tried to reproduce the same problem but not able to reproduce the error. One time error came at the firstline of the following code.

    Dim dsValidTables As WADataset = dsResult.Copy
    For Each dt As DataTable In dsValidTables.T ables
    If dt.TableName.St artsWith("DimSe l") Then
    If Not sValidTables.Co ntains(dt.Table Name) Then 'And Not dt.TableName.Eq uals("DimSelLis t") Then
    dsResult.Tables .Remove(dt.Tabl eName)
    End If
    End If
    Next

    StackTrace is

    [InvalidOperatio nException: Collection was modified; enumeration operation may not execute.]
    System.Collecti ons.ArrayListEn umeratorSimple. MoveNext() +2727902
    System.Data.Dat aSet.Copy() +328
    CB.WADatasource s.GetDatasource s(WAViewMessage aoViewMessage) in C:\Projects\WA\ WebApp\CB\WADat asources.vb:478
    CP.ServerContro ls.WAViewManage r.GetDatasource s(WAViewMessage aobjMsg) in C:\Projects\WA\ WebApp\CP.Serve rControls\WAVie wManager.vb:76
    DSS.WA.CPO.Serv erControls.WAVi ewManager.Refre sh() in C:\Projects\WA\ WebApp\CP.Serve rControls\WAVie wManager.vb:355
    WASoltnSV.Page_ Load(Object sender, EventArgs e) in C:\Solutions\WA 2\WASoltnSV.asp x.vb:19
    System.Web.UI.C ontrol.OnLoad(E ventArgs e) +99
    System.Web.UI.C ontrol.LoadRecu rsive() +47
    System.Web.UI.P age.ProcessRequ estMain(Boolean includeStagesBe foreAsyncPoint, Boolean includeStagesAf terAsyncPoint) +1061

    Any help on reproducing or solving the problem is greatly thankful.
  • Joseph Martell
    Recognized Expert New Member
    • Jan 2010
    • 198

    #2
    There 3 things to understand about this situation. First, when you use the For Each key words you are depending on the IEnumerable interface of a collection and hence an enumerator. Second, a restriction of using an enumerator to traverse a collection is that you can't modify a collection while you are enumerating through it without throwing an error. From MSDN documentation:

    "An enumerator remains valid as long as the collection remains unchanged. If changes are made to the collection, such as adding, modifying, or deleting elements, the enumerator is irrecoverably invalidated and the next call to MoveNext or Reset throws an InvalidOperatio nException."

    The third item is that copy can operate in one of 2 ways. It can be a shallow or a deep copy. A deep copy would create new objects for the dsValidTables object so that it is completely isolated from dsResult. A shallow copy would copy references. So even though dsValidTables and dsResult are different objects, the underlying objects they reference are the same. Your error would suggest that dsResult.copy is a shallow copy, giving you 2 objects that depend on the same Tables collection. Modifying one modifies the other and you wind up with your exception above.

    As an alternative, you could save the objects you want to remove in a separate collection, then use that list to remove items from dsValidTables.T ables (one of many solutions).

    Comment

    Working...