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

12

14

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.

Burgrave answered 3/8, 2011 at 7:13 Comment(0)
K
31

This most likely means that there is an error in your app.config file, e.g. badly formed XML or unexpected elements. The error happens because the static fields inside SqlConnection read from app.config to determine trace detail level as described here: http://msdn.microsoft.com/en-us/library/ms254503.aspx

Kev answered 6/9, 2011 at 21:37 Comment(2)
in my case i forgot to include the <configSections> tagLogography
@tennisBoy In my case just having empty content for configSections was screwing up. So application minds it badly if you have <configSections></configSections> in your app.config file.Mendicant
T
3

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.

Take answered 24/5, 2019 at 19:9 Comment(0)
L
2

Delete app.config and then run the program

Ligamentous answered 20/11, 2016 at 17:26 Comment(1)
This is a bad answer that may work for you. I am unsure why but this worked. Most likely a malformed App.Config but maybe it was something else.Moralist
B
1

I also faced that exception once when I reopened my project. In my case the Config file had empty tag and key as you can see below. I removed the appSettings tag to fix the problem.

< ?xml version="1.0" encoding="utf-8" ?>

< configuration>

< appSettings>

< add key="" value="site"/>

< /appSettings>

< configSections>

< /configSections>

< /configuration>

Bracy answered 5/7, 2012 at 10:0 Comment(0)
F
1

Check if you have the following Windows hotfix installed - KB2840628 - for the .NET FW 4.0 (use Windows Control Panel->Programs+Features and click 'view installed updates') and if so try uninstalling it...

https://connect.microsoft.com/VisualStudio/feedback/details/793500/sqlclr-typeinitializationexception-in-sqlconnection-since-kb2840628

It resolved the problem for me.

Formwork answered 12/8, 2013 at 16:54 Comment(1)
It's KB2840628 that creates problems if you have custom values inside <appSettings> in the app.config file ...Elver
E
1

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 ...

Ezechiel answered 28/3, 2015 at 12:36 Comment(0)
B
1

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.

Broach answered 17/7, 2019 at 7:56 Comment(0)
T
0

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

  • Users who only had just the old version of VMWare View Client installed have no problem.
  • Users who only had just the new version of VMWare View Client installed have no problem.
  • But users who had an old version of VMWare View Client, but have now been upgraded, do have a problem, and it does continue even if we uninstall VMWare View Client, then install the new version.

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:

  • If your user has local admin rights, this problem doesn't occur after upgrading VMWare View Client.
  • If they don't have local admin rights, the issue will occur.

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.

Thrush answered 25/4, 2016 at 7:50 Comment(0)
I
0

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.

Ictinus answered 20/9, 2018 at 11:45 Comment(0)
C
0

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.

Coloring answered 3/6, 2019 at 9:13 Comment(0)
W
0

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> 
Wheelock answered 7/2, 2020 at 7:46 Comment(0)
C
0

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

Cleary answered 13/2, 2020 at 16:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.