"Configuration system failed to initialize" error - c#

I'm getting this exception when using the code:
var appSettings = ConfigurationManager.AppSettings; //Exception here
string result = appSettings["ILFSsqlServer"] ?? "Not Found"; //trying to get to this point
I'm not really sure why and from previous answers to this question I think I'm doing exactly what they suggest but to no avail.
My app.config is:
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
</sectionGroup>
</configSections>
<AppSettings>
<add key="ILFSsqlServer" value="ODBC;DSN=sql server copycloas;Trusted_Connection=Yes;APP=Microsoft Office 2010;DATABASE=ILFSView;"/>
</AppSettings>
</configuration>

On your app.config you should write appSettings, lowercase A.

Related

I want to get different tags of App.config File in C#

I have to get string inside Setting-> Value in C#. How to reach there through code and fetch that value from App.config file. Please help me. Thank you.
<?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="MyApplication.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<applicationSettings>
<MyApplication.Properties.Settings>
**<setting name="ConnectionString" serializeAs="String">
<value>server=127.0.0.1;uid=root;pwd=root;database=MyApplication_db;</
</setting>**
</VASI_Application.Properties.Settings>
</applicationSettings>
</configuration>
You can use the following code:
var section = (ClientSettingsSection)ConfigurationManager.GetSection("applicationSettings/MyApplication.Properties.Settings");
var settingElement = section.Settings.Get("ConnectionString");
var settingValue = settingElement.Value.ValueXml.InnerText;
// settingValue equals "server=127.0.0.1;uid=root;pwd=root;database=MyApplication_db;"
First, you retrieve the ClientSettingsSection section. The name of the section is a combination of the name of the "applicationSettings" section and its inner "MyApplication.Properties.Settings" section. Then, you retrieve the actual settings by calling Settings.Get("ConnectionString") on the returned ClientSettingsSection instance. Finally, you can use the Value.ValueXml.InnerText property of the returned setting to get to the actual value.
By the way, you have two small errors in your XML file:
The <MyApplication.Properties.Settings> element in incorrectly closed with </VASI_Application.Properties.Settings>
The <value> element is not correctly closed with </value>
Don't you have the property ConnectionString?
As documented here, usually you can access it in this way:
var setting = Properties.Settings.Default.ConnectionString;
By the way, your application tag starts with MyApplication.Properties.Settings but ends with VASI_Application.Properties.Settings
If you do actually want to go ahead with a custom config file section like in your example then you will need to follow this Microsoft guide for creating custom configuration sections.
https://msdn.microsoft.com/en-us/library/2tw134k3.aspx
If you get stuck following this guide then raise another question.

How to update setting in app.config.exe file runtime

I am trying to update value of a particular setting in app.config.exe at run time. But as per my code its getting updating in vshost32.exe file which seem to duplicate as app.config.exe. My code is given below
private void btnSubmit_Click(object sender, EventArgs e)
{
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup applicationSectionGroup = config.GetSectionGroup("applicationSettings");
string d = applicationSectionGroup.Sections[0].ToString(); ;
ConfigurationSection applicationConfigSection = applicationSectionGroup.Sections["Secure_Browser_CS_Version.Properties.Settings"];
ClientSettingsSection clientSection = (ClientSettingsSection)applicationConfigSection;
//Encryption Key Configuration Setting
SettingElement applicationSetting = clientSection.Settings.Get("NavigateURL");
applicationSetting.Value.ValueXml.InnerXml = txtURL.Text;
applicationConfigSection.SectionInformation.ForceSave = true;
config.Save();
}
// app.config.exe
<?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="MyApplicationName.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<MyApplicationName.Properties.Settings>
<setting name="NavigateURL" serializeAs="String">
<value></value>
</setting>
</MyApplicationName.Properties.Settings>
</applicationSettings>
</configuration>
It seems you are doing everything correct, but since you are running using vshost.exe, this is the exe whose configuration is updated.
To run without vshost, uncheck the "Enable the Visual Studio hosting process" checkbox in Project prroperties -> Debug tab:

Storing settings in configFile

Try to save all data in to config file, and then read it and apply to my program during running - as user preference.
Whats done: create new config file (using Add new element-> add congif file). In this file put simple code
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSetting>
<add Key="Volume" value="100" />
</appSetting>
</configuration>
After it create method
public int GetVolumeFromConfigFile()
{
return Convert.ToInt32(ConfigurationManager.AppSettings.Get("Volume"));
}
and in main programm call it like
Value = (MyClass.GetVolumeFromConfigFile());
But it's not work. (During debaggin it's return nothing)
Think it can be few reason:
I need to add (I don't now in what way) what config file to use, because i have few files *.config - one as default (App.exe.config, and another - that i created)
I use incorrect method to get value from config file
Also I read about some another way to store app settings, like store it in *.settings file
What I'm doing wrong and what method prefered?
Additional information - use net 4.0
EDIT
Remove my config file, and add to existed few lines (in strong>)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="PlayDemo.SettingsPlayIt" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<userSettings>
<PlayDemo.SettingsPlayIt>
<setting name="Volume" serializeAs="String">
<value>10</value>
</setting>
</PlayDemo.SettingsPlayIt>
</userSettings>
Here I add my key
<appSetting>
<add key="Volume" value="100" />
</appSetting>
</configuration>
try this:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Volume" value="100" />
</appSettings>
</configuration>
and
return Convert.ToInt32(ConfigurationManager.AppSettings["Volume"]);
the appSettings are key value pairs, so you can access it like you would a value in a Dictionary
If you want to use a separate config file, try this:
Dim config As System.Configuration.Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
config.AppSettings.File = "yourFileName"'tell Configuration what file to read
config.Save(ConfigurationSaveMode.Modified) ' save the Configuration setting
ConfigurationManager.RefreshSection("appSettings") ' update just the <appSettings> node
I really like the following technique, using ConfigurationSection. This allows you for painless manipulation of your configuration. But it is more specific upfront.
http://msdn.microsoft.com/en-us/library/2tw134k3%28v=vs.90%29.aspx

Multiple AppSettings files, is it possible?

I want to create 3 AppSettings config files:
Database.config
Messages.config
Global.config
And after add in my App.config:
<appSettings file="Database.config" />
<appSettings file="Messages.config" />
<appSettings file="Global.config" />
But when I try to access a key that there is in one of three files with the ConfigurationManager, I got the following error:
Configuration system failed to initialize. Sections must only appear once per config file.
I cannot have more than one AppSettings config file?
You can't have more than one appsettings because that's the name of a section. You can add a new section though that uses the same kind of section definition as appsettings. E.g.,
<configuration>
<configSections>
<section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>
....
<DatabaseConfig>
<add key="Whatever" value="stuff"/>
</DatabaseConfig>
</configuration>
Code for separate file:
Web.config:
<configSections>
<section name="DatabaseConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="MessageConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
<section name="GlobalConfig" type="System.Configuration.NameValueFileSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<DatabaseConfig configSource="database.config">
</DatabaseConfig>
<MessageConfig configSource="message.config">
</MessageConfig>
<GlobalConfig configSource="global.config">
</GlobalConfig>
database.config:
<DatabaseConfig>
<add key="Name" value="ServerName" />
</DatabaseConfig>
etc...
Can be accessed via code like this:
var databaseConfiguration = (NameValueCollection)ConfigurationManager.GetSection("DatabaseConfig");
string name = databaseConfiguration["Name"];

can't read variables out of app.config

what I have in app.config is this
<?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="Porject.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup>
<applicationSettings>
<Porject.Properties.Settings>
<setting name="PhotoLocation" serializeAs="String">
<value>.\photos\</value>
</setting>
</Porject.Properties.Settings>
</applicationSettings>
<connectionStrings>
<add name="****" connectionString="Data Source=***;Initial Catalog=****;User ID=***;Password=***" />
</connectionStrings>
</configuration>
and this is how I call the PhotoLocation:
string s = ConfigurationManager.AppSettings["PhotoLocation"];
When I make a request to get the connectionstring, there is no problem and it works fine; but when I requst the PhotoLocation it returns null.
Also the ConfigurationManager.AppSettings.Count returns 0.
Does anyone have an idea on what I'm doing wrong?
simply add this in your App config file
<appSettings>
<add key="PhotoLocation" value=".\photos\"/>
<appSettings>
ConfigurationManager.AppSettings reads (as the name might suggest) the AppSettings block of your configuration.
Given you've created your own section, you want to use var section = ConfigurationManager.GetSection("Porject.Properties.Settings") and read the values from that section.

Categories

Resources