Connection string from app.config in windows application

To solve "The name ‘ConfigurationManager’ does not exist in the current context" problem


1 minute read

In windows application configuration file, connection strings for your database can be stored as key/value pairs in the connectionStrings section of the configuration element.

    <add name="MyDBConnection" connectionString="Server=xxxx; UID=xxxx; PWD=xxxx; Database=xxxx;" />

To access your connection string from the configuration file you need to use the following code.

string connectionString = ConfigurationManager.ConnectionStrings["MyDBConnection"].ConnectionString;

The ConfigurationManager class in included with in the System.Configuration namespace.

using System.Configuration;

After the using statement included, compiler says that

“The name ‘ConfigurationManager’ does not exist in the current context”.

To resolve this problem you need to add reference to “C:\Windows\Microsoft.NET\Framework<Version Folder>\System.configuration.dll” using following steps. (Eg: C:\Windows\Microsoft.NET\Framework\v4.0.30319\System.Configuration.dll) Goto Project -> Add References…

Switch to “Assemblies” tab in ‘Add Reference’ window, and select System.Configuration and click OK to add the reference in your project. (You can search “System.Configuration” to locate the reference)

Now the connection string from the app.config file will be accessed using the above code.

comments powered by Disqus