views:

601

answers:

2

Recently I came across the error "Collection was modified after the enumerator was instantiated" and I have no idea why. The error specifies the web.config. What causes this error?

I haven't been able to reproduce it. The error pops up very seldom and random. My program takes in a file and processes it. When I receive this error I simply rerun the program and it is fine.

I'm thinking something is modifying my web.config while my program is running? My program does connect to several databases (sql, db2 and oracle) so at times it can take several seconds to complete. Could something touching the web.config during that time be the cause or am I looking at the wrong area?

Edit:

Here is some additional information about the error:

Type : System.Configuration.ConfigurationErrorsException, System.Configuration,    Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Message : An error occurred loading a configuration file: Collection was modified after the enumerator was instantiated. (<path>\web.config line 59)
Source : System.Configuration
Help link :
BareMessage : An error occurred loading a configuration file: Collection was modified after the enumerator was instantiated.
Filename : <path>\web.config
Line : 59
Errors : System.Configuration.ConfigurationException[]
Data : System.Collections.ListDictionaryInternal

Looking at the stack trace it happened when I tried to read a connection string from the web.config and line 59 contains the connection string information that is being read.

Does this still look like it's due to modifying a collection in a loop?

I do add items to a collection but not in a loop and nowhere near where the stack trace is showing. I don't remove any items from collections.

+5  A: 

You cannot change a collection while iterating over it (i.e. in a foreach).

So, adding or removing items from a collection while within the foreach block will cause this error.

As an error in web.config it might be that you are adding a duplicate key that is defined elsewhere, or that you are trying to change the config dynamically in your code.

Oded
+Instead iterate backwards through the collection using an integer index. This will allow you to do deletes if necessary.
Spencer Ruport
I doubt this error has anything to do with the config file. Look at the collections you are processing with a foreach and you should be able to find the problem.
Jeff Siver
If this is the case wouldn't the error happen consistently? This error happens very rarely and I have yet to be able to make the error show up even when I run the exact same file through the process.
metanaito
Are you writing anything into the config file?
Oded
+1  A: 

the reference to the web.config is probably to enable detailed errors. If you follow the instructions shown, it will show you the page and line number where the error occoured. This error is commonly caused either from (as mentioned earlier) modifying the collection inside of an itterator like this:

List<string> myList = new List<string>();
myList.Add(some strings);
foreach(string s in myList){
  myList.Remove(s); // <-- EXCEPTION 
}