1

I am currently working on a simple app using Ms Sql and VSC# desktop app. Then this occurs:

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception

I tried figuring out how to solve it but to no avail. I already tried adding this as the first child of configuration tag in app.config

<configSections>
    <sectionGroup name="applicationSettings"
        type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
        <section name="YourProjectName.Properties.Settings"
            type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
            requirePermission="false" />
    </sectionGroup>
</configSections>

Still it didnt work. Then I remembered that if you install vmware or XAMPP and MS Sql Management studio, this error occurs. Isnt there any other way to make these two work without uninstalling the other?

UPDATE: I tried the 'self-enclosing tag' of add and this happened e

this is my whole config file

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <sectionGroup name="applicationSettings"
                  type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >

      <section name="ToDoListDesktopMsSql.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               requirePermission="false" />

    </sectionGroup>
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
  </startup>
  <add name="TodoContext" providerName="System.Data.SqlClient"
         connectionString="Data Source=QLYNE-PC\QLYNESQL; Initial Catalog=ToDoListDb; integrated security = true;"
         />


</configuration>
Pamingkas Sevada
  • 432
  • 9
  • 21

1 Answers1

0

This problem often occurs because of invalid XML in the app.config file, such as forgetting that add tags should be self-closing. For example, using this:

<add key="ConnectionString" value="Data Source=server name;...">

Instead of this:

<add key="ConnectionString" value="Data Source=server name;..." />

Have you confirmed that your app.config file is valid XML?

See also previous question/answer, Exception: type initializer for 'system.data.sqlclient.sqlconnection'?

AlwaysLearning
  • 7,915
  • 5
  • 27
  • 35