CoffeeCode
CoffeeCode

Reputation: 4314

Getting the connectionstring in .net 4

I want to get a connection string from the app.config file.

connectionString = System.Configuration.ConfigurationSettings.AppSettings["DBEntities"];

But it doesn't work. It's empty.

I cannot access the System.Configuration.ConfigurationManager because it's .NET 4. How can I get my connection string from the app.config?

Upvotes: 30

Views: 48270

Answers (3)

Nealv
Nealv

Reputation: 6884

In .net 4 you have to use:

ConfigurationManager.ConnectionStrings["name of connection string in web.config"]

More about it is here and here.

Upvotes: 8

sh_kamalh
sh_kamalh

Reputation: 3901

Use

string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString; 

Make sure to add reference to System.configuration in your project.

Upvotes: 79

vc 74
vc 74

Reputation: 38179

Add a reference to System.Configuration to your project and use ConnectionStrings instead of AppSettings

Upvotes: 5

Related Questions