1

I have a Windows Form application in VS 2013 and I'm trying to establish a database connection. Every time I run the program, however, I receive the following error:

An unhandled exception of 'System.Configuration.ConfigurationErrorsException' occurred in         
System.Configuration.dll

Additional information: Configuration system failed to initialize

Here's what my files look like:

Program.cs

using System;
using System.Collections;
....

namespace ProgramName
{
    static class FakeNameB
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}

Form1.cs

using System;
....

namespace ProgramName
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            string CS = System.Configuration.ConfigurationManager.ConnectionStrings[<nameInApp.config>].ConnectionString.ToString();

            InitializeComponent();
        }
    }
}

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="ProgramName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections> 
    <configurationStrings>
        <add name = <name> connectionString = "Data Source=<dataSource>;Initial Catalog=<catalog>;Integrated Security=True;" providerName="System.Data.SqlClient"></add> 
    </configurationStrings>
</configuration>

None of the suggestions in this post seemed to help, and I was not able to reference System.Configuration.dll like this post suggested. Any ideas?

Community
  • 1
  • 1
Adam Freymiller
  • 1,929
  • 7
  • 27
  • 49

2 Answers2

4

The fix to this particular issue could be found in the comments of the accepted answer to this post, and was resolved by moving the configSections tag to be the first child of and moving the startup tag to be the last section included.

Community
  • 1
  • 1
Adam Freymiller
  • 1,929
  • 7
  • 27
  • 49
3

In your app.config, it should be <connectionStrings> not <configurationStrings>:

<connectionStrings>
    <add name="[name]" connectionString="[string]" providerName="System.Data.SqlClient" /> 
</connectionStrings>
mxmissile
  • 11,464
  • 3
  • 53
  • 79