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:02

I started to get this problem after uninstalling Oracle Client Drivers and it removed my C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\machine.config!

Copying it from another computer resolved the problem.

查看更多
聊天终结者
3楼-- · 2019-01-03 03:04

I know this has already been answered but I had exactly the same problem in my unit tests. I was tearing my hair out - adding an appSettings section, and then declaring the configuration section as per the answer. Finally found out that I had already declared an appSettings section further up my config file. Both sections pointed to my external settings file "appSettings.config" but the first appSettings element using the attribute file whilst the other used the attribute configSource. I know the question was about the connectionStrings. Sure enough, this happens if the appSettings element is the connectionStrings element being duplicated with different attributes.

Hopefully, this can provide someone else with the solution before they go down the path I did which leads to wasting an hour or two. sigh oh the life of us developers. We waste more hours some days debugging than we spend developing!

查看更多
你好瞎i
4楼-- · 2019-01-03 03:06
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="xyz" value="123" />    
  </appSettings>
</configuration>
查看更多
Viruses.
5楼-- · 2019-01-03 03:07

If you are dealing with an Azure WebJob - I had to remove the following after upgrading to the latest 4.6.1.

  <compilation debug="true" targetFramework="4.6.1">
    <assemblies>
      <add assembly="System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
    </assemblies>
  </compilation>

Hope this helps.

查看更多
该账号已被封号
6楼-- · 2019-01-03 03:11

It is worth noting that if you add things like connection strings into the app.config, that if you add items outside of the defined config sections, that it will not immediately complain, but when you try and access it, that you may then get the above errors.

Collapse all major sections and make sure there are no items outside the defined ones. Obvious, when you have actually spotted it.

查看更多
Anthone
7楼-- · 2019-01-03 03:12

Delete old configuration files from c:\Users\username\AppData\Local\appname and c:\Users\username\AppData\Roaming\appname and then try to restart your application.

查看更多
登录 后发表回答