aspdotnet-suresh offers articles and tutorials,csharp dot net, articles and tutorials,VB.NET Articles,Gridview articles,code examples of 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

Read/Write appsettings from web.config file in asp net

Mar 31, 2012

In this article I will explain how to read or write appsettings in web.config file using


In Previous article I explained clearly about how to read or write connectionstrings from web.config file in Now I will explain how to write appsettings in web.config file and how to read appsettings from web.config file using 

My application contains many pages and each page interacts with database to get data and display it on page for that I need to declare database connections in each page. After connection declaration in all pages suppose if server name or credentials of database has changed in that situation I need to modify database connections in all pages. 

To solve this problem I decided to place database connection in one place and reuse it in every page wherever we need to connect to SQL Server. I decided to place appsettings string values in web.config.

To add appsettings first create new website open web.config file and search for “appSettings” and add new item in appSettings section

After open web.config file in application add sample db connection in appSettings section like this 

<add key="yourappsettingsstringName" value=" yourappsettingsvalue" />
Example of declaring appSettings in web.config file like this

<add key="dbconnection" value="Data SOurce=SureshDasari;Initial Catalog=MysampleDB; Integrated Security=true"/>
After add dbconnection in appsettings we need to write the some code in our codebehind file to get this dbconnection string from web.config file.

To test this one first create new website and add following code in aspx page

<html xmlns="">
<head runat="server">
<title>Read or Write appsetting values from web.config file in</title>
<form id="form1" runat="server">
<asp:Label ID="lblText" runat="server"/>
After that add following namespace in codebehind file

using System;
using System.Configuration;
 The namespace using System.Configuration; is used to get configuration section details from web.config file.

After add namespaces write the following code in code behind

C# code

protected void Page_Load(object sender, EventArgs e)
//Get appsettings string value from web.config file
string strcon = ConfigurationManager.AppSettings["dbconnection"];
//Bind appsettings string value to label
lblText.Text = strcon;

Imports System.Configuration
Partial Class VBSample
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
'Get appsettings string value from web.config file
Dim strcon As String = ConfigurationManager.AppSettings("dbconnection")
'Bind appsettings string value to label
lblText.Text = strcon
End Sub
End Class
Here I am saving database connection you can use save any value and use it anywhere in application. Suppose if you need to use some variable value in all page then it would be better to declare that variable in appsettings section in web.config file and we can use it in anywhere in application.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest,, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email


birkin said...

I tried over and over times again, but I still can't write appsettings in web.config file. Where the problem is?

aadhar sarvaiya said...

can you please tell how to change connection string using C# with user interface

Anonymous said...

Thanks dude !

Anonymous said...

About this article, it shows how to read, but never how to write. Change the article's title to make it less frustrating one.

Give your Valuable Comments

Other Related Posts

© 2015 All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.