30

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?

2
  • can you post your web.config with the connectionstring section Commented Oct 15, 2010 at 10:57
  • ConfigurationManager isnt an option in .net 4 Commented Feb 20, 2012 at 17:39

3 Answers 3

79

Use

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

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

Sign up to request clarification or add additional context in comments.

Comments

8

In .net 4 you have to use:

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

More about it is here and here.

1 Comment

+1 but the question is tagged with c# so instead of () you want []
5

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.