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.
source to share
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.
source to share
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>
source to share