title: What causes the error "Collection was modified after the enumerator was instantiated"?
body: <p>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?</p>
  
  <p>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.</p>
  
  <p>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?</p>
  
  <p>Edit:</p>
  
  <p>Here is some additional information about the error:</p>
  
  <pre><code>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. (&lt;path&gt;\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 : &lt;path&gt;\web.config
  Line : 59
  Errors : System.Configuration.ConfigurationException[]
  Data : System.Collections.ListDictionaryInternal
  </code></pre>
  
  <p>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.</p>
  
  <p>Does this still look like it's due to modifying a collection in a loop?</p>
  
  <p>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.</p>