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

9
  • 2
    What is the message of the exception? Commented Feb 8, 2019 at 7:55
  • Its type initializer doesn't really do much, but it does attempt to access config. You'd think sufficiently broken config would stop your program completely but maybe worth looking through your config and seeing if there's anything suspect there. Commented Feb 8, 2019 at 8:06
  • @Damien_The_Unbeliever i'm not using it at all so it has only one tag with framework ver, already tried deleting it and recreating Commented Feb 8, 2019 at 8:09
  • Does the inner exception have a stack trace as well as a message? Commented Feb 8, 2019 at 8:11
  • Why do you create the connection object before the connection string? In fact, why use SqlConnectionStringBuilder when you don't modify the initial connection string? Commented Feb 8, 2019 at 8:23

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

it worked, but not straght away, the error shifted to conn.Open(), I installed the oldest nuget possible, tried everything actually, but after a reboot it started working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.