Reputation: 189
I've got this error message when I trying to get the parameter from the app.config, below is my config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<ConnectionStrings>
<add name="conStr" ConnectionString="this is connection string" ></add>
</ConnectionStrings>
</configuration>
in C# I wrote like that:
string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
Console.WriteLine(str);
so who knows where I did wrong?
Thanks!
Upvotes: 1
Views: 2362
Reputation: 57202
The case is wrong, it should be connectionStrings
with a lowercase c
.
If you look at the InnerException
property of the exception you are getting, you will see that it says: "Unrecognized configuration section ConnectionStrings. (... line 3)".
To see the inner exception, you can either step in with the debugger or use this code:
using System;
using System.Configuration;
class Program {
static void Main(string[] args) {
try {
string str = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
Console.WriteLine(str);
} catch (ConfigurationErrorsException exc) {
Console.WriteLine(exc.Message);
if (exc.InnerException != null) {
Console.WriteLine(exc.InnerException.Message);
}
}
}
}
Your App.Config file should look like this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="conStr" connectionString="this is the connection string" />
</connectionStrings>
</configuration>
With the config file like this, I get no errors. If you are still getting an error, there is something else wrong in your App.config, and you should analyze the contents of the inner exception to find out what it is.
Upvotes: 2
Reputation: 21171
I think the connectionstrings tag needs to start with a lowercase c:
<connectionStrings>
....
</connectionStrings>
Upvotes: 0