SqlConnection() parameterless exception

0

I get this error trying to initialize SqlConnection instance.

First I tried with ConnectionString parameter, now I see it occurs even without it in the constructor. The code used to work, but fails after I changed my PC, so I suppose it has something to do with windows settings (windows 7) or user rights My code:

using (SqlConnection conn = new SqlConnection())
{
   SqlConnectionStringBuilder builder = 
   new SqlConnectionStringBuilder("Server=server1;Integrated Security=SSPI;Initial Catalog=db1");
   conn.ConnectionString = builder.ConnectionString;
}

I already tried switching target framework back and forth as some suggest with no result (using 4.5.2 at the moment)

update

The exception thrown on the using line:

System.TypeInitializationException occurred HResult=0x80131534 Message=The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception. Source= StackTrace: at System.Data.SqlClient.SqlConnection..ctor() at Reg_CB_Report.Program.GetSql(String ExecText) in H:\MY\code\c#\Reg_CB_Report\Reg_CB_Report\Program.cs:line 228 at Reg_CB_Report.Program.Main(String[] args) in H:\MY\code\c#\Reg_CB_Report\Reg_CB_Report\Program.cs:line 83

Inner Exception 1: ArgumentException: The parameter is incorrect. (Exception from HRESULT: 0x80070057 (E_INVALIDARG))

update2

App.config:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/>
    </startup>
</configuration>

tried recreating it - no result

StackTrace:

StackTrace " at System.Security.Policy.PEFileEvidenceFactory.GetLocationEvidence(SafePEFileHandle peFile, SecurityZone& zone, StringHandleOnStack retUrl)\r\n at System.Security.Policy.PEFileEvidenceFactory.GenerateLocationEvidence()\r\n at System.Security.Policy.PEFileEvidenceFactory.GenerateEvidence(Type evidenceType)\r\n at System.Security.Policy.AssemblyEvidenceFactory.GenerateEvidence(Type evidenceType)\r\n at System.Security.Policy.Evidence.GetHostEvidenceNoLock(Type type)\r\n at System.Security.Policy.Evidence.GetHostEvidence(Type type, Boolean markDelayEvaluatedEvidenceUsed)\r\n at System.Security.Policy.AppDomainEvidenceFactory.GenerateEvidence(Type evidenceType)\r\n at System.Security.Policy.Evidence.GetHostEvidenceNoLock(Type type)\r\n at System.Security.Policy.Evidence.RawEvidenceEnumerator.MoveNext()\r\n at System.Security.Policy.Evidence.EvidenceEnumerator.MoveNext()\r\n at System.Configuration.ClientConfigPaths.GetEvidenceInfo(AppDomain appDomain, String exePath, String& typeName)\r\n at System.Configuration.ClientConfigPaths.GetTypeAndHashSuffix(AppDomain appDomain, String exePath)\r\n at System.Configuration.ClientConfigPaths..ctor(String exePath, Boolean includeUserConfig)\r\n at System.Configuration.ClientConfigPaths.GetPaths(String exePath, Boolean includeUserConfig)\r\n at System.Configuration.ClientConfigurationHost.RequireCompleteInit(IInternalConfigRecord record)\r\n at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)\r\n at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)\r\n at System.Data.SqlClient.SqlConnection..cctor()" string

c#
.net
asked on Stack Overflow Feb 8, 2019 by Vadim • edited Feb 8, 2019 by kara

1 Answer

2

It looks like you're trying to use the latest version of the SqlClient NuGet package (4.6) on the oldest supported runtime. A missing security update may be involved as well.

The type initializer mentioned in the exception is the static constructor which tries to load the SqlColumnEncryptionEnclaveProviders configuration section. I've never encountered that section either.

One option is to go back to an earlier SqlClient package that works. Another option is target .NET 4.7.2 and later. Finally, you can add the missing section yourself, as the link to the SharePoint bug shows :

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <configSections>
       <section name="SqlColumnEncryptionEnclaveProviders" 
         type="System.Data.SqlClient.SqlColumnEncryptionEnclaveProviderConfigurationSection, System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" /> 
    </configSections>
</configuration>

This isn't the only bug introduced by SqlClient 4.6. There was another one, again involving the parameterless constructor.

answered on Stack Overflow Feb 8, 2019 by Panagiotis Kanavos • edited Feb 8, 2019 by Panagiotis Kanavos

User contributions licensed under CC BY-SA 3.0