This week I came across a very strange error. We have recently migrated an application, developed with the .NET 1.1 Framework, to the .NET 2.0 Framework. This application uses a SqlConnection object to establish a connection to a Sql 2000 server or a Sql 2005 server.
When trying to open the connection, an exception of type System.TypeInitializationException occured:
The type initializer for ‘System.Data.SqlClient.SqlConnection’ threw an exception.
at System.Data.SqlClient.SqlConnection..ctor(String connectionString)
I was somewhat surprised to get this error, because this program always worked fine (under .NET v1.1).
As Visual Studio 2005 shows an error box when an unhandled exception occurs, I was able to review the inner exceptions. Unfortunately only the details of the Exception.InnerException can be viewed. The details of the Exception.InnerException.InnerException (if any) can not be viewed here. So the only thing I could see was that System.Data.SqlClient.SqlPerformanceCounters threw an exception.
By searching the web I was able to find a forum were a similar problem was discussed. In this forum a suggestion was made that this problem was linked to an unrecognized element in the application configuration file.
Although I was a bit skeptic about the proposed solution, I reviewed my application configuration file. I noticed that Visual Studio did indeed not recognize part of the configuration specified.
As this configuration (registration of a trace listener) was no longer needed I removed it, and the application executed as it was supposed to. J
Looking for some more information on the type System.Data.SqlClient.SqlPerformanceCounters in combination with the System.Data.SqlCllient.SqlConnection I was surprised not to find any in the MSDN(2) documentation. It does not show up in the intellisense in Visual Studio 2005 either.
Using Reflector I was able to determine that the type does really exist in System.Data.dll for the .NET 2.0 Framework. It did not in the .NET 1.1 Framework. The use of performance counters was added in this version of the .NET framework. See also this article on the MSDN site: http://msdn2.microsoft.com/en-us/library/ms254503.aspx