none
DbProviderFactory CreateConnection Fails for System.Data.SqlClient RRS feed

  • Question

  • Here is the code to test with. Create a new Form, add a button and put this code in the button.

            Dim dbf As Data.Common.DbProviderFactory
            '
            ' uncomment one of the below dbf = 
            '
            'this fails
            'dbf = Data.Common.DbProviderFactories.GetFactory("System.Data.SqlClient")
            'these work
            'dbf = Data.Common.DbProviderFactories.GetFactory("System.Data.Odbc")
            'dbf = Data.Common.DbProviderFactories.GetFactory("System.Data.OleDb")
    
            Dim conn As Data.Common.DbConnection
            conn = dbf.CreateConnection
    

    The create connection only fails for SqlClient.  The error is

    System.TypeInitializationException was unhandled
      HResult=-2146233036
      Message=The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.
      Source=System.Data
      TypeName=System.Data.SqlClient.SqlConnection
      StackTrace:
           at System.Data.SqlClient.SqlConnection..ctor()
           at System.Data.SqlClient.SqlClientFactory.CreateConnection()
           at WindowsApplication1.Form1.Button1_Click(Object sender, EventArgs e) in C:\Users\dewayne.basnett\AppData\Local\Temporary Projects\WindowsApplication1\Form1.vb:line 19
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.PerformClick()
           at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
           at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
           at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
           at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
           at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
           at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
           at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
           at WindowsApplication1.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: System.InvalidOperationException
           HResult=-2146233079
           Message=Failed to read the configuration section for enclave providers. Make sure the section is correctly formatted in your application configuration file. Error Message: Configuration system failed to initialize
           Source=System.Data
           StackTrace:
                at System.Data.SqlClient.SqlConnection..cctor()
           InnerException: System.Configuration.ConfigurationErrorsException
                BareMessage=Configuration system failed to initialize
                HResult=-2146232062
                Line=0
                Message=Configuration system failed to initialize
                Source=System.Configuration
                StackTrace:
                     at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
                     at System.Configuration.Internal.InternalConfigRoot.OnConfigRemoved(InternalConfigEventArgs e)
                     at System.Configuration.Internal.InternalConfigRoot.RemoveConfigImpl(String configPath, BaseConfigurationRecord configRecord)
                     at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(String configKey, Boolean getLkg, Boolean checkPermission, Boolean getRuntimeObject, Boolean requestIsHere, Object& result, Object& resultRuntimeObject)
                     at System.Configuration.BaseConfigurationRecord.GetSection(String configKey)
                     at System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)
                     at System.Data.SqlClient.SqlConnection..cctor()
                InnerException: System.Configuration.ConfigurationErrorsException
                     BareMessage=Unrecognized configuration section userSettings.
                     Filename=C:\Users\dewayne.basnett\AppData\Local\WindowsApplication1\WindowsApplication1.vshos_Url_odwpvb4iq0tpdwxwwhwkttkzifz10zja\1.0.0.0\user.config
                     HResult=-2146232062
                     Line=3
                     Message=Unrecognized configuration section userSettings. (C:\Users\dewayne.basnett\AppData\Local\WindowsApplication1\WindowsApplication1.vshos_Url_odwpvb4iq0tpdwxwwhwkttkzifz10zja\1.0.0.0\user.config line 3)
                     Source=System.Configuration
                     StackTrace:
                          at System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
                          at System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)
                          at System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()
                          at System.Configuration.ClientConfigurationSystem.OnConfigRemoved(Object sender, InternalConfigEventArgs e)
                     InnerException: 
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Wednesday, August 15, 2018 2:06 PM

Answers

  • The answer, and I am not sure of the mechanics behind this, is...

    Yesterday a user in another domain was trying to install one of my apps and setup was throwing an error. It looked like a permission problem but they insisted it was not. One of their programmers suggested that I change the properties of a code file from Do not copy to Copy always. I didn't think it would help, but they insisted. Eventually I figured out that it was how they were mapping to the install directory.

    They had it as \fileserver.mo.gov\install. Changing it to \fileserver\install fixed the problem. I did not change the code file back.

    As this problem became more bizarre I thought of what happened yesterday and remembered the change. I set it back to Do not copy, republished the app, and the above problem went away. As I said I don't understand the mechanics behind this, but my issue is resolved.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    • Marked as answer by dbasnett Wednesday, August 15, 2018 5:29 PM
    Wednesday, August 15, 2018 5:29 PM

All replies

  • Greetings,

    Targeting 4.6.1 Framework the following works

    Imports System.Data.Common
    
    Public Class Form1
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim provider As String = "System.Data.SqlClient"
            Dim df As DbProviderFactory = DbProviderFactories.GetFactory(provider)
    
            Using cnn As DbConnection = df.CreateConnection
                cnn.ConnectionString = "Data Source=.\SQLEXPRESS;" &
                                       "Initial Catalog=ApplicationProductionData;" &
                                       "Integrated Security=True"
                cnn.Open()
                MessageBox.Show("Open")
            End Using
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, August 15, 2018 2:45 PM
    Moderator
  • Hey K.!  When I encountered the error I thought I had done this before.  I went back and looked at some old code that I knew worked and it now fails.  I am beginning to believe that something on my machine has changed.

    FWIW - all of the code I have targets 4.5 and has for a very long time.

    Not sure where to start looking.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.


    • Edited by dbasnett Wednesday, August 15, 2018 3:14 PM
    Wednesday, August 15, 2018 3:13 PM
  • Yeah if it worked before and now it's not "something" had to change. I just tried the code with 3.5 and 4.5, still worked. Wonder if the version of Visual Studio has something to do with this?

    FYI I'm running on Windows 10.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Wednesday, August 15, 2018 3:46 PM
    Moderator
  • Yeah if it worked before and now it's not "something" had to change. I just tried the code with 3.5 and 4.5, still worked. Wonder if the version of Visual Studio has something to do with this?

    FYI I'm running on Windows 10.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites


    Hard to believe but I added a setting to projects 'Settings'.  Guess what, it works now.

    I looked at the user.config file from the exception.  It looked like

    <?xml version="1.0" encoding="utf-8"?> <configuration> <userSettings> <WindowsApplication1.My.MySettings> <setting name="AutoIncrement" serializeAs="String"> <value>False</value> </setting> </WindowsApplication1.My.MySettings> </userSettings> </configuration>

    It gets better.  At the same time I created another identical forms app.  Guess what.  It ran while the first failed.  The second one's user config looked like this

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
            <sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
                <section name="CityCountyView.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
                <section name="StatDwnLoad.My.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
            </sectionGroup>
        </configSections>
        <userSettings>
            <CityCountyView.My.MySettings>
                <setting name="newVers" serializeAs="String">
                    <value>False</value>
                </setting>
                <setting name="wState" serializeAs="String">
                    <value>Normal</value>
                </setting>
                <setting name="wLoc" serializeAs="String">
                    <value>573, 615</value>
                </setting>
                <setting name="wSize" serializeAs="String">
                    <value>840, 566</value>
                </setting>
            </CityCountyView.My.MySettings>
            <StatDwnLoad.My.MySettings>
                <setting name="newVers" serializeAs="String">
                    <value>False</value>
                </setting>
                <setting name="autoadd" serializeAs="String">
                    <value>True</value>
                </setting>
                <setting name="wState" serializeAs="String">
                    <value>Normal</value>
                </setting>
                <setting name="wLoc" serializeAs="String">
                    <value>573, 5</value>
                </setting>
                <setting name="wSize" serializeAs="String">
                    <value>1068, 596</value>
                </setting>
                <setting name="WPorWD" serializeAs="String">
                    <value>CL</value>
                </setting>
                <setting name="showBDS" serializeAs="String">
                    <value>True</value>
                </setting>
                <setting name="HorS" serializeAs="String">
                    <value>H</value>
                </setting>
            </StatDwnLoad.My.MySettings>
        </userSettings>
    </configuration>

    I have no idea where that came from.  I am confused!


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    Wednesday, August 15, 2018 4:06 PM
  • The answer, and I am not sure of the mechanics behind this, is...

    Yesterday a user in another domain was trying to install one of my apps and setup was throwing an error. It looked like a permission problem but they insisted it was not. One of their programmers suggested that I change the properties of a code file from Do not copy to Copy always. I didn't think it would help, but they insisted. Eventually I figured out that it was how they were mapping to the install directory.

    They had it as \fileserver.mo.gov\install. Changing it to \fileserver\install fixed the problem. I did not change the code file back.

    As this problem became more bizarre I thought of what happened yesterday and remembered the change. I set it back to Do not copy, republished the app, and the above problem went away. As I said I don't understand the mechanics behind this, but my issue is resolved.


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it."

    - from former MSDN User JohnWein

    SerialPort Info

    Multics - An OS ahead of its time.

    • Marked as answer by dbasnett Wednesday, August 15, 2018 5:29 PM
    Wednesday, August 15, 2018 5:29 PM