问题
In my C# application, i am trying to connect to a DataConnection. The application is just a simple Login form but as soon as the user clicks the login button, it throws an exception error referencing the Connection String. Can anyone help?
public void userLoginSuccessfull()
{
try
{
//////////////////////////////////
// This line is throwing the error
//////////////////////////////////
string connString = System.Configuration.ConfigurationManager
.ConnectionStrings["connectionString"].ConnectionString;
if (txtUsername.Text != "" & txtPassword.Text != "")
{
string queryText = @"SELECT Count(*) FROM Users
WHERE Username = @Username AND Password = @Password";
using (SqlConnection cn = new SqlConnection(connString))
using (SqlCommand cmd = new SqlCommand(queryText, cn))
{
cn.Open();
cmd.Parameters.AddWithValue("@Username", txtUsername.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
int result = (int)cmd.ExecuteScalar();
if (result > 0)
{
loadUserForm();
}
}
}
}
catch (Exception ee)
{
Console.WriteLine(ee.StackTrace);
}
}
This is the error
'EnviroWaste Job Logger.vshost.exe' (CLR v4.0.30319: EnviroWaste Job Logger.vshost.exe): Loaded 'C:\Users\listm\Documents\Visual Studio 2013\Projects\EnviroWaste Job Logger\EnviroWaste Job Logger\bin\Debug\EnviroWaste Job Logger.exe'. Symbols loaded.
A first chance exception of type 'System.Configuration.ConfigurationErrorsException' occurred in System.Configuration.dll
Configuration system failed to initialize
A first chance exception of type 'System.TypeInitializationException' occurred in System.Data.dll
The connection string looks like this:
connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\UsersDatabase.mdf;Integrated Security=True"
回答1:
The error you got relates to a configuration file that has incorrect content. Most likely, your app.config
(if desktop) or web.config
(if web app) file the following error:
It does not have as its first element the
<configSections>
element.
Check that file and make sure it looks something like this
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- Section groups, and stuff like userSettings, etc -->
</configSections>
<connectionStrings>
<add name="connectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\UsersDatabase.mdf;Integrated Security=True"/>
</connectionStrings>
<!-- Possibly other stuff -->
</configuration>
回答2:
Also ran into the same problem. The explanation Alex provided gives a clue into the issue however it is not enough.
The app.config apparently expects the elements to appear in a certain order.
Therefore, in app.config, right after <configuration>, the first section MUST be:
<configSections>
<!-- Section groups, and stuff like userSettings, etc -->
</configSections>
Followed by the
<connectionStrings>
<add name="connectionString" connectionString="...whatever is here..."/>
</connectionStrings>
And then comes the rest.
In my case, I had:
<system.data>
<DbProviderFactories>
<remove invariant="System.Data.SQLite.EF6" />
<add name="SQLite Data Provider (Entity Framework 6)" invariant="System.Data.SQLite.EF6" description=".NET Framework Data Provider for SQLite (Entity Framework 6)" type="System.Data.SQLite.EF6.SQLiteProviderFactory, System.Data.SQLite.EF6" />
</DbProviderFactories>
As the very first element after <config>.
All I needed to was to move the <system.data> section and place it AFTER the <connectionStrings>, in order to respect the order that VS needs.
So in addition to needing to have the <configSections> before <connectionString> these must also be the two first sections right after <config>. This is what fixed it for me.
来源:https://stackoverflow.com/questions/29835821/retrieving-a-connectionstring-through-system-configuration-throws-exception