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
?
config file in the Views folder.) Find the <connectionStrings> element: Add the following connection string to the <connectionStrings> element in the Web. config file.
To read the connection string into your code, use the ConfigurationManager class. string connStr = ConfigurationManager. ConnectionStrings["myConnectionString"].
Connection strings can be stored as key/value pairs in the connectionStrings section of the configuration element of an application configuration file. Child elements include add, clear, and remove. The following configuration file fragment demonstrates the schema and syntax for storing a connection string.
Use
string connectionString = ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;
Make sure to add reference to System.configuration
in your project.
In .net 4 you have to use:
ConfigurationManager.ConnectionStrings["name of connection string in web.config"]
More about it is here and here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With