Home > ASP.NET > Get AppSettings value from code behind

Get AppSettings value from code behind

When you are developing an ASP.NET web application, you sometimes may need to reuse a term or name over and over again across the application. If the term is used in some places only, there might not be a problem to change it when you need to change the term.

However, if the application that you are developing is a big application, it will be very tedious if you need to change the term value after lots of pages has been coded. So how do you solve this?

The solution is to use AppSettings which can be located in the web.config. Values that has been added into the AppSettings tag in the web.config file can be accessed across the entire application. So, any changes made to the value of the AppSettings, it will be applied across the entire application without having to change the values page by page.

Below, I’ll be showing you how to access the AppSettings values you have added into the web.config file from the code behind file. You can also access this values from class file in your application.


Note: I have added a Label with the ID of “Label1” in the Default.aspx file.


Code in web.config:

        <add key="MyAppSettings" value="Some value" />

Code for C#:

Label1.Text = System.Configuration.ConfigurationManager.AppSettings["MyAppSettings"].ToString();

Code for VB.NET:

Label1.Text = System.Configuration.ConfigurationManager.AppSettings("MyAppSettings").ToString()


The sample code above shows you how to get the value of the key “MyAppSettings” from the collection of pre defined application settings from the AppSettings tag in the web.config file. Then it displays the value out to the Label with ID “Label1”.


Note: Make sure that your web application has reference to System.Configuration. Otherwise, you will get errors when you compile your web application.

  1. No comments yet.
  1. December 26, 2010 at 10:50 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: