I'm trying to get my key value set in the appsettings.Config file but seems not working.
This is what i wrote for that. The code is called from the constructor of an MDI file and its returning only null value. Anybody know why?
var getValue = ConfigurationSettings.AppSettings["ShowQueryTextbox"];
I also tried with ConfigurationManager.AppSettings . That too didnt work.
My AppSettings Code is as follows.
<configuration>
<appSettings>
<add key="ShowQueryTextbox" value="true"/>
</appSettings>
</configuration>
Read Key value from WebConfig using C# string constring = ConfigurationManager. ConnectionStrings["ABCD"]. ConnectionString; using (SqlConnection con = new SqlConnection(constring)) { //do database operations like read table data or save data. }
To access these values, there is one static class named ConfigurationManager, which has one getter property named AppSettings. We can just pass the key inside the AppSettings and get the desired value from AppSettings section, as shown below. When we implement the code given above, we get the output, as shown below.
ConfigurationSettings.AppSettings are obsolete, try
ConfigurationManager.AppSettings["ShowQueryTextbox"];
Remember that to use:
ConfigurationManager.AppSettings["MyKey"];
You need to add reference to System.Configuration to your project.
The issue arise on renaming the App.Config file as AppSettings.Config. Thanks for all the guidances and help.
The ConfigurationManager
is still up to date - Year 2017.
Btw, if you simply want to convert the appsettings configuration value from string to bool, then use Convert.ToBoolean
if (Convert.ToBoolean(ConfigurationManager.AppSettings["EnableLoggingInfo"]))
{
log.Info(message);
}
In your appsettings configuration (web.config)
<appSettings>
<add key="EnableLoggingInfo" value="true" />
</appSettings>
I am able to get like this:
System.Configuration.ConfigurationManager.AppSettings.Get("KEY").ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With