The following connectionString is present in App.Config
<configuration>
<appSettings>
<add key="ConnectionString" value="Data Source=machine1;Initial Catalog=database1;User Id=user1;Password=password1;"/>
</appSettings>
</configuration>
Now I would like to retrieve it using the following code
public static string GetConnectionString
{
get
{
return ConfigurationManager.AppSettings["ConnectionString"];
}
}
Is this correct
Thanks

App.Config
Dennisld
But it returns null.
p.s this is in .NET 2.0
Can you see why pls
Thanks
hharding
Does the ConfigurationSettings class work and is the name of the config file <executablename>.exe.config
KK Wong
Andrew_Shough
Hi,
As mentioned in the first post
this returns null
Thanks
Morg
As i said before, the ConfigurationSettings class is obsulated. But i was wondering if it was working with that class.
Are you sure that the config file is located in you directory where the executable is located and the the name of the config file is <executablename>.exe.config
Janni J
vanita
ccchai
please note that I am using .net 2.0
An it is not the same I don't think.
Thanks
Sherif Elian
System.Configuration.ConfigurationManager.AppSettings
BillH
so, how do you re-write this in .NET 2.0
Thanks
Chris Cormier
it returns null because the file that keeps the configuration it is NOT in the same directory as the main assembly. if you created the configuration file by adding it to the project it is initialy located in the base directory of your application. in order to work it must be in the DEBUG directory of the main assembly, if you are in debuging mode. If you are in the release form of the application the configuration file must be in the RELEASE directory of the main asembly.
Roberson
So not sure what the problem is.
Thanks
webGov
<configuration>
<appSettings>
<add key="mykey" value="value" />
</appSettings>
</configuration>
System.Configuration.ConfigurationSettings.AppSettings["mykey"]