Data Platform Developer Center > Data Platform Development Forums > ADO.NET Data Providers > The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
Ask a questionAsk a question
 

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

  • Tuesday, June 07, 2005 6:58 AMDavid Tatton Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Test it on a virtual PC running SQL 2000 on a Win 2003 PKLATFORM.
    Throws the above error on win 2000 machine with a networked SQL 2000 server
    Is this an AD thing???

All Replies

  • Wednesday, October 05, 2005 8:56 PMSiraj Lala - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    This could be due to missing references. Or it could be due to an error in the app config file. Is it possible for you to post your code?
  • Wednesday, June 13, 2007 7:00 AMMars_Ram Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    hey it may be the problem with app.config settings.

    u hv to code like this

     

    <appSettings>

    <add key ="ConnectionString" value="Data Source=server name;Initial Catalog=db name;User ID= sa;Password= sa"/>

    </appSettings>

     

    if u code this wrongly ,typeintializer error w'll throw.

    Also if the key name is used different from app.config also exception will throw.

     

  • Thursday, August 02, 2007 8:29 PMJeffMlakar Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Yes, check your settings to see if you have one that is a connection string that is conflicting with the one you have.  I had the same problem and that fixed it.

  • Tuesday, October 09, 2007 6:42 AMsourvil Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,
    I got the same error. I use windows authentication. And i have several web.config files on each sub folder to authenticate users by their groups. And my web.config file on this folder is just like this:

    Code Block

    <?xml version="1.0"?>
    <!--
        Note: As an alternative to hand editing this file you can use the
        web admin tool to configure settings for your application. Use
        the Website->Asp.Net Configuration option in Visual Studio.
        A full list of settings and comments can be found in
        machine.config.comments usually located in
        \Windows\Microsoft.Net\Framework\v2.x\Config
    -->
    <configuration>
        <appSettings/>
        <connectionStrings/>
        <system.web>

        </system.web>
    </configuration>


    And i can run several dts and sp with same (Microsoft) sqlhelper.cs class. But this gives me the error.
    How can i solve this?

    Regards..

  • Wednesday, October 10, 2007 6:12 AMBahman; Fakhr Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    OK, This is very shocked me! when i reopen my project and run it, I face this Exception but in fact the Config File (exp : app.config) contains this wrong Information and when I correct it the problom solved!

     

    -- Contains Key that is not set yet (Line No 3,4,5)

     

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

    <configuration>

    <appSettings>

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

    </appSettings>

    <configSections>

    </configSections>

    </configuration>

     

    -- Correct One

     

    <configuration>

    <configSections>

    </configSections>

    </configuration>

     

     

    Just this !!!

  • Wednesday, October 10, 2007 9:39 AMsourvil Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I change config file but ttill getting same error.
  • Monday, October 15, 2007 5:50 AMsourvil Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    No help?
  • Wednesday, May 14, 2008 12:10 PMTimothy Patton Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I'm getting the same error message but in a different scenario.  I have a .NET DLL with COM interop that we call from a VB6 application.  When I run the app from the VB6 IDE, everything works fine.  But when I create an EXE and run from that, the exception is thrown on this line of code in the DLL:

     

    SqlConnection cn = new SqlConnection("Data Source=serverName;Initial Catalog=S2;uid=userID;pwd=password;");

     

    The exception.ToString() returns the following:

     

    System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
    ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnectionFactory' threw an exception.
    ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception.
    ---> System.PlatformNotSupportedException: This operation is only supported on Windows 2000 SP3 or later operating systems.
       at System.Security.Principal.SecurityIdentifier..ctor(WellKnownSidType sidType, SecurityIdentifier domainSid)
       at System.Diagnostics.SharedUtils.EnterMutexWithoutGlobal(String mutexName, Mutex& mutex)
       at System.Diagnostics.SharedPerformanceCounter.GetCounter(String counterName, String instanceName, Boolean enableReuse, PerformanceCounterInstanceLifetime lifetime)
       at System.Diagnostics.SharedPerformanceCounter..ctor(String catName, String counterName, String instanceName, P

     

    Anyone have any thoughts on this?

  • Sunday, May 25, 2008 11:33 AMxentolln Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    Yeah this is one i run into here and there usually when i try be smart and create a connection as a property of a class..

    move it into a method  presto bye bye...

    eg

     

     

    class foo()

    {

    // dont make it a property.

    Sql connection con = new  con(conStr).. bla bla ( BAD  dont over think stuff.. code like a lamo )

     

    int bar(int i)

    {

    //***move it to here and test..

    //so not a propety any more move it here..

    SqlConnection con....

    }

     

    Un huh...

  • Friday, January 09, 2009 8:12 PMCSharpDev Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I faced a similar problem when I was working with WCF, which is set to allow impersonation.
     
    Two things i did solved my problem
    1. On the wcf config file I corrected my connection string.
    2. On the client side of app I set
    ServiceClient..ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
  • Thursday, January 15, 2009 5:36 PMPaul F. Wood Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code

    If you are using VS 2008 with WCF (and especially with regard to integrated security usage) can I recommend the above from CSharpDev and if that doesn't work, use this as your perrenial fall back for fixing what appears to be a Visual Studio with WCF problem.

    Open a VS 2008 Command Prompt and use this:

    aspnet_regiis -i

    And then:

    "%windir%\Microsoft.NET\Framework\v3.0\Windows Communication Foundation/ServiceModelReg.exe" -i -x

    And then:

    iisreset

    This should sort out your problems.

    However I must admit that on one machine I use this is a problem and not on another (so far), so it may well be something in the PC's build.

  • Wednesday, August 26, 2009 9:04 AMcup_of_tea Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed Answer
    I found replacing the machine.config file resolved the error. It can become corrupted. Lift it from a working machine.
    C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config
    • Proposed As Answer bycup_of_tea Thursday, August 27, 2009 9:45 AM
    •  
  • Tuesday, September 15, 2009 8:40 AMDilC85 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Check the COM component and .DLL is available in the installation folder wher .exe exists. make sure you are referencing COM component from your installation directory or from any other where with the correct path. Else you have to register the component. See more exception help http://dileepsol.blogspot.com.  
  • Thursday, November 05, 2009 6:47 AMVikramBapat Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hey I faced similar problem but moving the <ConfigSections> Node moved after <Configuration> node resolves the issue.
  • Friday, November 13, 2009 11:54 PMelwehishy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I faced this problem and tried all the above 
    but
    no solution 
    are there anyone help me???????

    thanks
  • Saturday, November 14, 2009 3:16 PMcup_of_tea Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    I have faced this is a number of times. Sometimes it can be an installation of the .net framework that is at fault (usually 2.0). I have used the following tool to remove .net 2.0 and then reinstall from it from Microsoft.

    http://blogs.msdn.com/astebner/pages/8904493.aspx

    Worked for me twice !!! :)

  • Saturday, November 14, 2009 9:41 PMelwehishy Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    ok 
    thanks for your care cup_of_tea

    i found my problem
    my problem is the application is not read the connection string in app.config so when i start with debugging, it is all write but when start without debugging the exception is thrown 
    another thing 
    when i clear the comming from machine.config as the following code 

    <configuration>
      <clear/>
        <connectionStrings>
            <add name="ConnectionString"
                connectionString="Data Source=.;Initial Catalog='Computer Sales System';Integrated Security=True"/>
        </connectionStrings>
    </configuration>

    the exception is thrown in both sides 
    so 
    i understand that the connection string in app.config is not read by application 
    so i want to know why???????? and what is the solution for this problem

    thanks
  • Sunday, November 15, 2009 7:55 PMcup_of_tea Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    It sounds like there are multiple versions of the app.config file. When you build the application (in release) make sure the config file is copied to the output directory and that your application points to the correct file.

    You can set the app.configs properties to 'always copy to output directory'. Also there may be a cached version of the file somewhere so I would search the web for how to clear caches etc. I think you can do 'Clean Solution' from build menu. Good luck.
  • Tuesday, November 24, 2009 1:25 AMMarkMMullin Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    If anyone sees this

    Had this problem with a WPF application calling a web service using entity framework - on the same wcf server had an existing web service, also using entity framework which had no problems - wcf had all the attributes for impersonation

    On the wcf rich client, added the following line right after i created the client and all was well

    _catalogService

     

    .ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Delegation;