By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,717 Members | 1,268 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,717 IT Pros & Developers. It's quick & easy.

Collection was modified; enumeration operation may not execute.

P: 1
Hi all,

I am working on 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.Tables
If dt.TableName.StartsWith("DimSel") Then
If Not sValidTables.Contains(dt.TableName) Then 'And Not dt.TableName.Equals("DimSelList") Then
End If
End If

StackTrace is

[InvalidOperationException: Collection was modified; enumeration operation may not execute.]
System.Collections.ArrayListEnumeratorSimple.MoveN ext() +2727902
System.Data.DataSet.Copy() +328
CB.WADatasources.GetDatasources(WAViewMessage aoViewMessage) in C:\Projects\WA\WebApp\CB\WADatasources.vb:478
CP.ServerControls.WAViewManager.GetDatasources(WAV iewMessage aobjMsg) in C:\Projects\WA\WebApp\CP.ServerControls\WAViewMana ger.vb:76
DSS.WA.CPO.ServerControls.WAViewManager.Refresh() in C:\Projects\WA\WebApp\CP.ServerControls\WAViewMana ger.vb:355
WASoltnSV.Page_Load(Object sender, EventArgs e) in C:\Solutions\WA2\WASoltnSV.aspx.vb:19
System.Web.UI.Control.OnLoad(EventArgs e) +99
System.Web.UI.Control.LoadRecursive() +47
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1061

Any help on reproducing or solving the problem is greatly thankful.
Aug 6 '10 #1
Share this Question
Share on Google+
1 Reply

Joseph Martell
Expert 100+
P: 196
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 InvalidOperationException."

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.Tables (one of many solutions).
Aug 6 '10 #2

Post your reply

Sign in to post your reply or Sign up for a free account.