Exception: type initializer for 'system.data.sqlclient.sqlconnection'?

Viewed 58799

I couldn't figure out what is the problem for this exception.

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception

First attempt: I was using the WCF Service to make some small application. It works just fine and I can use the LINQ properly. After 2 or 3 days. Maybe after I close Visual studio and load the project again. The exception appear.

The 2nd attempt also the same. I create another project and it work just fine until I do something else (I do not change any code)

SQL Server working fine and I can connect through SQL Management Studio without problem.

A click at Debug error lead me to the connection string from linq file.

What could be the problem? I tried to search but couldn't find the answer to resolve this.

Thank you

12 Answers

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:

  • Oracle.ManagedDataAccess.Core
  • System.Data.SqlClient

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.

I have the same error. And I findout the solution for it by change "app.config":

<configuration>
  <configSections>
    <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
      <section name="O4E.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false"/>
    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v2.0.50727"/>
  </startup>
  <applicationSettings>
    <O4E.Settings>
      <setting name="abc" serializeAs="String">
        <value>xyz</value>
      </setting>
    </O4E.Settings>
  </applicationSettings>
  <appSettings>
    <add key="" value=""/>
    <add key="" value=""/>
  </appSettings>
</configuration>

Note: Line [supportedRuntime version="v2.0.50727"] is the most importance.

sorry for the late response on this, but after investigating this issue for almost 3 days in a row I thought I'd share my results. I had the same error pop up in SQL Server Reporting Services, more specifically the web portal after getting HTTP 503 Service Unavailable errors on my browsers.

Turns out that the issue was related to both, the service account that runs the SSRS instance plus the account that connects to the database, which in my case was located on another server than the reporting instance. I haven't yet been able to pinpoint exactly where the problem lies inside the configuration and/or user rights, i.e. where do the accounts exactly need elevated rights. All in all seems that the underlying problem was that the account I was running the web portal code under, didn't have enough rights to call on the System.Data.SQLClient type.

TL;DR: Check your users permissions! Hope this helps someone, I had a long 3 days trying to figure this out.

A similar solution to those mentioned above, but in my case it was the applicationSettings tag within the app.config that needed to be removed.

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> 

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");

Related