Configuration System Failed to Initialize

2019-01-03 02:26发布

I'm new to Visual Studio. I'm currently creating a Login form.

I have this code.

string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try
{
    using (OdbcConnection connect = new OdbcConnection(connectionString))
    {
        connect.Open();
        OdbcCommand cmd = new OdbcCommand("SELECT username, password FROM receptionist", connect);
        OdbcDataReader reader = cmd.ExecuteReader();

        if (username_login.Text == username && password_login.Text == password)
        {
            this.Hide();
            MessageBox.Show("Invalid User", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            this.Close();
        }
        else 
            MessageBox.Show("Invalid User", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        connect.Close();
    }
}
catch (OdbcException ex)
{
    MessageBox.Show(ex.Message, "error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

But whenever I try to type in the username and password there is an error called Configuration system failed to initialize. I'm just wondering what kind of problem is this and how could I solve this?

Please help.

19条回答
混吃等死
2楼-- · 2019-01-03 03:12

I solved the problem by using the below code

   <?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="YourProjectName.Properties.Settings"
               type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
               requirePermission="false" />

    </sectionGroup>

  </configSections>
  <appSettings>
    <add key="SPUserName" value="TestUser" />
    <add key="SPPassword" value="UserPWD" />
  </appSettings>
</configuration>
查看更多
我想做一个坏孩纸
3楼-- · 2019-01-03 03:17

This is kinda dumb, but for me I fixed it by doing a get latest from source control on my code. I think there was some new configuration element that was added by someone else, and I needed to overwrite my configuration files. OP shows the error I had gotten, which wasn't really pointing me in the right direction.

查看更多
够拽才男人
4楼-- · 2019-01-03 03:19

In my case, within my .edmx file I had run the 'Update Model From Database' command. This command added an unnecessary connection string to my app.config file. I deleted that connection string and all was good again.

查看更多
Animai°情兽
5楼-- · 2019-01-03 03:20

I had this same problem with an MSTest class: Marlon Grech in his article says "the element has to be defined as the first element in the App.config."

So make sure that is the first element in under the element. I had put AppSettings first.

查看更多
【Aperson】
6楼-- · 2019-01-03 03:21

Make sure that your config file (web.config if web, or app.config if windows) in your project starts as:

<?xml version="1.0"?>
<configuration>
    <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>
</configuration>

Note that inside the configuration element, the first child must be the configSections element.

In the name property on section element, make sure you replace YourProjectName with your actual project's name.

It happened to me that I created a webservice in a class library project, then I copied (overwriting) the config file (in order to bring the endpoints configuration) to my windows app and I started to have the same problem. I had inadvertently removed configSections.

it worked for me, hope it helps

查看更多
放荡不羁爱自由
7楼-- · 2019-01-03 03:21

Sometimes the Error occurs because a windows create a duplicate in the

C:\Users\App Data\Local\"You App Name"...

Just delete this folder and done. try it.

查看更多
登录 后发表回答