I was coding a (windows) tool for offical purpose for which I wanted to provide configurable customization xml data as input to the tool. My obvious choice (I thought) was the App.Config file. I decided to put up the following structure for my purpose:
<LookupName value = "somevalue">
<Columns><Column><Source>MySource1</Source>
<Destination>MyDestination1</Destination>
</Column></Columns></LookupName>
[Note: the above xml is for reference only, so please ignore any typos in there. The problem has nothing to do with the problem in the XML]
After putting in the logic to parse the xml in the code behind, all seemed to work fine when I was stepping through the code. However, when I took off all the break points and hit an F5 ...bOOM!!! A runtime error popped up in my code at the following line - "SQLConnection connSQL = new SQLConnection("with connection string purposely hard-coded);". I was left dumb founded for a while, because that's the last thing where I would expect an error to pop up in my code. Since I couldn't believe my eyes, I was optimistic enough to re-run the code to give the compiler and the runtime a chance to realize that it had wronged! Jokes apart...it didn't help. So I decided to drill into the inner exception to discover a new exception which baffled me even more: "The type initializer for System.Data.SqlClient.SqlPerformanceCounters threw an exception".
After some trial and error, an interesting dicovery surfaced. It seems that the App.Config file cannot contain custom xml like the one declared above, unless we write a handler class for the custom configuration that inherits from ConfigurationSection class. So if I do not wish to write a ConfigurationSection class, the only option that remains is to take the custom XML out into another XML file and remove it from the App.Config.
However, I still fail to understand, why in the first place didn't I get some error like "Invalid entry in the application configuration file" Or "Cannot parse App.Config" Or something of that sorts? Why did I get a "Type Initialization Error" that pointed to the SQLConnection object when I wasn't even referring to the App.Config (that's why I hard coded the connection string....to ensure that I could isolate the issue and prove to my ownself that showing a Type Initialization Error on SQLConnection has no rhyme or reason !!!!)
For the moment I found a work-around (wouldn't exactly say a resolution), however I will post more here if I discover more. Till then ciao.