How to store and retrieve settings from appSettings

This is how you store and retrieve settings in your web.config on Microsoft's .NET 4.0. This also works in Mono.

First add a reference in your project to "System.Configuration"

Change your web.config like this:

<appSettings><add key="ConnectionString" value="server=(local);database=Northwind;Integrated Security=SSPI" /></appSettings>


And change your code into this:

var connectionString = System.Configuration.ConfigurationManager.AppSettings["ConnectionString"];

using(var connection = new SqlConnection(connectionString))
{
    connection.Open();
    // ...
}

Comments