I couldn\'t figure out what is the problem for this exception.
The type initializer for \'System.Data.SqlClient.SqlConnection\' threw an exception
I have the same error and the problem was in Web.config. In my case, just add the connectionStrings tag and its value and problem solved.
<connectionStrings>
your connection strings
</connectionStrings>
Delete app.config and then run the program
I've read a lot about this issue, but no one has mentioned the following cause of this error.
Some of our users, who had an older version of VMWare View Client which was then upgraded, are now seeing this issue.
The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception
We did attempt to reinstall .Net Framework and Visual Studio Tools for Office, but this didn't fix anything.
We haven't found a fix yet, but I hope this helps other users.
Btw, we did check our app.config
's and web.config
's, and these are all fine (and work perfectly with the rest of our users).
One other discovery:
So... we're now stuck.
I have a test laptop, and I can reproduce the error everytime when my user ID doesn't have Local Admin rights. If we then grant Local Admin rights, and I log in again, the error goes away.
And I added a load of try..catch's in my code, and found that the exception occurs in this one line of code:
MikesDataContext dc = new MikesDataContext()
So, it's actually making the connection which throws the exception. It does not get as far as the next lines of code which use this connection.
In my case , the problem was modifying tag name in app.config file by mistake just compare your app.config file with another old one .. You will find out the problem ...
I have run across this issue on programs that work fine for months, but sudden stop working. The fix I have found works every time is to place this as the first call in your Main().
System.Configuration.ConfigurationManager.GetSection("system.xml/xmlReader");
If you have a project that contains the NuGet packages for both Oracle and SQL Server, this solution might help you fix this error.
Uninstall both of these NuGet packages:
Open your app.config and check if you still have have any dependent assembly entries listed. If you do, delete those entries now. They might look something like this:
<dependentAssembly>
<assemblyIdentity name="Oracle.ManagedDataAccess" publicKeyToken="89b483f429c47342" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-2.0.19.1" newVersion="2.0.19.1" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Data.SqlClient" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-4.5.0.1" newVersion="4.5.0.1" />
</dependentAssembly>
While still in your app.config file, delete the entire Oracle Managed Data Access section if it exists (This might be the only thing that you need to do to fix this problem, however we performed all of the steps listed in this solution). If you have it, it might look something like this:
<oracle.manageddataaccess.client>
<version number="*">
<settings>
<!-- your TNS_ADMIN value would be located here -->
</settings>
<dataSources>
<dataSource alias="SampleDataSource" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=tcp)(HOST=localhost)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=ORCL))) " />
</dataSources>
</version>
</oracle.manageddataaccess.client>
Open the packages.config file and double check to ensure that the entries for the Oracle and SQL Server packages are not still listed - they should be gone, but it doesn't hurt to be 100% sure.
Save your project, and then re-add the NuGet packages.