SqlConnection throws an exception

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

SqlConnection connection = new SqlConnection( SQLCONNECTION_STRING );

      

It works fine on my development machine, but throws an exception on a Windows 2003 server. The application goes through CGI and is Full Trust. I tried multiple connection strings and I think the string is not causing the problem because even this code gets an exception:

SqlConnection connection = new SqlConnection();

      

Thank.


I discovered two strange facts:

  • It works if I attach a debugger to the process and simple code tracing.
  • It works if I run the application from the command line (not via CGI if required).

So, I think there is something wrong with the CGI and SqlConnection interactions. Does anyone know about this?

Thanks everyone for the answers.


Edited to add:

Here is my connection string: "Provider = SQLOLEDB; Data Source = (local); Home Directory = Users; User ID = User; Password = Password;"

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

See below for the exception:

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 an inconspicuous constructor throws an exception. So I don't think the problem is with the connection string. I can see that the stack trace says "Illegal characters in the path". So I will try to figure it out and find out. But maybe someone already knows the solution.

+2


source to share


7 replies


This issue is usually related to a problem in your config file. You might want to recreate this.



Thank you, Jivtesh

+7


source


I had this problem where mine app.config

included an empty connection string like:

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

      

Because of this sound, you have a version that launches both a console app and a web app. This means there will be an asp.net site for a console application as app.config

well web.config

.



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

to match yours app.config

.

Also, you might want to consider the various rights inherent in the system - the website may not have permission to connect to the database as it works like IUSR_<machinename>

, but the console application runs under your own user credentials and you are probably the administrator of the machine.

+2


source


Try the following:

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

      

0


source


For me it was the wrong web.config trace configuration, I had the error

System.Diagnostincs.TextWriterTraceListener

instead

System.Diagnostics.TextWriterTraceListener

and also there were two microcircuit elements ... fixing those two issues helped to solve my problem.

0


source


If the service is available from a browser, check the client-side endpoint behavior configuration as:

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

      

0


source


The error mentioned in SqlConnection connection = new SqlConnection();

is correct, but due to a bug in App.Config, the error message is misleading.

0


source


Checking for internal exceptions will tell you what the exact problem is with your config file, you may need to dig a little deeper.

0


source







All Articles