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

Popular posts from this blog

Switching from Docker Desktop to Rancher Desktop on WSL2: Solving the Missing docker-credential-desktop.exe Error

Running LXD/LXC on WSL2 with Ubuntu 20.04

Proxmox Remote administration via VNC