Slava
Slava

Reputation: 300

SqlConnection throws an exception

Dear professionals please help me with the following problem. In my .NET C# application a have this code:

SqlConnection connection = new SqlConnection( SQLCONNECTION_STRING );

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:

SqlConnection connection = new SqlConnection();

Thanks.


I found two weird facts:

  1. It works if I connect debugger to process and simple trace the code.
  2. It works if I run the application from command line (not through CGI as required).

So I guess something wrong with CGI and SqlConnection interaction. Does someone know about that?

Thanks everybody for responses.


Edited to add:

Here is my connection string: "Provider=SQLOLEDB;Data Source=(local);Initial Catalog=Users;User Id=user;Password=password;"

I've also tried several possible variants as described there: http://www.connectionstrings.com/sql-server

Please find information regarding the exception below:

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. ---> System.TypeInitializationException: The type initializer for 'System.Data.SqlClient.SqlPerformanceCounters' threw an exception. ---> System.Configuration.ConfigurationErrorsException: Configuration system failed to initialize ---> 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()

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.

Upvotes: 4

Views: 18377

Answers (7)

robin.lo
robin.lo

Reputation: 91

If the service is accesss able from the Browser, check the client side endpoint behavior configuration as :

<endpointBehaviors>
        <behavior name="clientEndpoint">
          <dataContractSerializer maxItemsInObjectGraph="2147483647" />
          <clientCredentials>
            <windows allowedImpersonationLevel="Delegation"/>
          </clientCredentials>
        </behavior>
</endpointBehaviors>

Upvotes: 0

Akbar Khan
Akbar Khan

Reputation: 408

Checking the Inner Exceptions will tell what is the exact problem with your config file, you might need to do some drill down deep.

Upvotes: 0

BMG
BMG

Reputation: 489

The error thrown at the SqlConnection connection = new SqlConnection(); is correct but it is due to the error in App.Config the error message misleads.

Upvotes: 0

Steve Cooper
Steve Cooper

Reputation: 21490

I had this problem when my app.config included an empty connection string section, like so;

<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
    </connectionStrings>
</configuration>

From the sound of it, you've got a version that runs as both a console app, and a web app. That means you'll have an app.config for the console app, and a web.config for the asp.net site.

Since you have a working console app, try to spot differences between the equivalent sections and update the web.config to match your app.config.

Also, you might consider the different permissions inherent in the system -- the web site might not have permission to connect to the database because it is running as IUSR_<machinename>, but the console app is running under your own user credentials, and you are probably a machine administrator.

Upvotes: 2

Eugenio Mir&#243;
Eugenio Mir&#243;

Reputation: 2428

For me it was web.config trace misconfiguration, I had a misspelled

System.Diagnostincs.TextWriterTraceListener

instead of

System.Diagnostics.TextWriterTraceListener

and also had two trace elements... fixing those two issues solved my problem.

Upvotes: 0

Jivtesh
Jivtesh

Reputation: 101

This problem is generally related to a problem in your configuration file. You might want to re-create that.

Thanks, Jivtesh

Upvotes: 9

Himadri
Himadri

Reputation: 8876

Try the following:

string str="Data Source=[YourServer];Initial Catalog=yourdb;User ID=sa;Password=sa;";
SqlConnection connection = new SqlConnection(str);

Upvotes: 0

Related Questions