title: SqlConnection throws an exception
body: <p>Dear professionals please help me with the following problem.
  In my .NET C# application a have this code:</p>
  
  <pre><code>SqlConnection connection = new SqlConnection( SQLCONNECTION_STRING );
  </code></pre>
  
  <p>It works marvelous on my development machine but throws an exception on Windows 2003 server. The application runs through CGI and has “Full trust” level.
  I've tried several connections strings and I think the string doesn't cause the issue because even this code gets an exception:</p>
  
  <pre><code>SqlConnection connection = new SqlConnection();
  </code></pre>
  
  <p>Thanks.</p>
  
  <hr>
  
  <p>I found two weird facts:</p>
  
  <ol>
  <li>It works if I connect debugger to process and simple trace the code.</li>
  <li>It works if I run the application from command line (not through CGI as required).</li>
  </ol>
  
  <p>So I guess something wrong with CGI and SqlConnection interaction. Does someone know about that?</p>
  
  <p>Thanks everybody for responses.</p>
  
  <hr>
  
  <p>Edited to add:</p>
  
  <p>Here is my connection string:
  "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=Users;User Id=user;Password=password;"</p>
  
  <p>I've also tried several possible variants as described there: <a href="http://www.connectionstrings.com/sql-server" rel="nofollow noreferrer">http://www.connectionstrings.com/sql-server</a></p>
  
  <p>Please find information regarding the exception below:</p>
  
  <pre><code>The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
     at System.Data.SqlClient.SqlConnection..ctor()
     at Test.Database.UpdateSQLServerDatabase(IDictionary`2 fields, String regName, StringBuilder regCode)
     at Test.Program.Run()
  
  Type: System.TypeInitializationException
  
  InnerException: System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlConnectionFactory' threw an exception. ---&gt; System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception. ---&gt; System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---&gt; System.ArgumentException: Illegal characters in path.
     at System.Security.Permissions.FileIOPermission.HasIllegalCharacters(String[] str)
     at System.Security.Permissions.FileIOPermission.AddPathList(FileIOPermissionAccess access, AccessControlActions control, String[] pathListOrig, Boolean checkForDuplicates, Boolean needFullPath, Boolean copyPathList)
     at System.Security.Permissions.FileIOPermission..ctor(FileIOPermissionAccess access, String path)
     at System.AppDomainSetup.VerifyDir(String dir, Boolean normalize)
     at System.AppDomainSetup.get_ConfigurationFile()
     at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)
     at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)
     at System.Configuration.ClientConfigurationHost.get_ConfigPaths()
     at System.Configuration.ClientConfigurationHost.GetStreamName(String configPath)
     at System.Configuration.ClientConfigurationSystem..ctor()
     at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
     --- End of inner exception stack trace ---
     at System.Configuration.ConfigurationManager.EnsureConfigurationSystem()
     at System.Configuration.ConfigurationManager.GetSection(String sectionName)
     at System.Configuration.PrivilegedConfigurationManager.GetSection(String sectionName)
     at System.Diagnostics.DiagnosticsConfiguration.GetConfigSection()
     at System.Diagnostics.DiagnosticsConfiguration.Initialize()
     at System.Diagnostics.Switch.InitializeConfigSettings()
     at System.Diagnostics.Switch.InitializeWithStatus()
     at System.Diagnostics.Switch.get_SwitchSetting()
     at System.Diagnostics.TraceSwitch.get_Level()
     at System.Data.ProviderBase.DbConnectionPoolCounters..ctor(String categoryName, String categoryHelp)
     at System.Data.SqlClient.SqlPerformanceCounters..ctor()
     at System.Data.SqlClient.SqlPerformanceCounters..cctor()
     --- End of inner exception stack trace ---
     at System.Data.SqlClient.SqlConnectionFactory..ctor()
     at System.Data.SqlClient.SqlConnectionFactory..cctor()
     --- End of inner exception stack trace ---
     at System.Data.SqlClient.SqlConnection..cctor()
  </code></pre>
  
  <p>Even parameterless constructor gives an exception. So I don't think the problem is in the connection string. I can see stack trace tells something about "Illegal characters in path". So I will try to drill down and find out. But maybe someone already knows solution.</p>