Problem with config file


G

Guest

Hi,

Am having trouble retrieving a value from a config file using VC# 2005.

app.config file (as created using 'Settings' tab on Project properties page)
is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings"
type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="TestSQL.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</sectionGroup>
</configSections>
<applicationSettings>
<TestSQL.Properties.Settings>
<setting name="SQLDatabase" serializeAs="String">
<value>server=(local);uid='sa';pwd=''</value>
</setting>
</TestSQL.Properties.Settings>
</applicationSettings>
</configuration>

The code I am using to try to access the SQLDatabase value is:

AppSettingsReader appReader = new AppSettingsReader();
string conn = (string)appReader.GetValue("SQLDatabase", typeof(string));

When the second line is executed, I get an InvalidOperationException saying
that "The key 'SQLDatabase' does not exist in the appSettings configuration
section."

Except it does exist. Cant see anything wrong - no problem with case even -
but I wonder about the namespace in the app.config file that encloses by
value. This corresponds with the default namespace for the project, which is
an ordinary WindowsApplication.

Any idea why I am getting this problem?

Thanks

Ian
 
Ad

Advertisements

G

Guest

Ian,
If you want to put the value in the appSettings section, you can do so easily:

<appSettings>
<add key="SqlDatabase" value="server=(local);uid='sa';pwd='' />
</appSettings>

Read this with: ConfigurationManager.AppSettings["SqlDataBase"];

You also have a connectionStrings section you can use for this.

Keep it simple if all you need is simple values.
Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top