Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 532
DucDigital Avatar asked Aug 03 '11 07:08

DucDigital


People also ask

What is System data SqlClient SqlConnection?

A SqlConnection object represents a unique session to a SQL Server data source. With a client/server database system, it is equivalent to a network connection to the server. SqlConnection is used together with SqlDataAdapter and SqlCommand to increase performance when connecting to a Microsoft SQL Server database.

What is SqlConnection and SqlCommand in C#?

SqlConnection and SqlCommand are classes of a connected architecture and found in the System. Data. SqlClient namespace. The SqlConnection class makes a connection with the database. Further, this connection (database connection) is used by the SqlCommand to work with that database.

How do I declare SqlConnection?

A SqlConnection is an object, just like any other C# object. Most of the time, you just declare and instantiate the SqlConnection all at the same time, as shown below: SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

When should you use the SqlConnection object?

If you want to access a database multiple times, you should establish a connection using the Connection object. You can also make a connection to a database by passing a connection string via a Command or Recordset object. However, this type of connection is only good for one specific, single query.


2 Answers

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

like image 172
Jared Moore Avatar answered Sep 18 '22 09:09

Jared Moore


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.

like image 30
ThePeter Avatar answered Sep 20 '22 09:09

ThePeter