Introduction:
In this article I will explain how to read or write appsettings in web.config file using asp.net.
In this article I will explain how to read or write appsettings in web.config file using asp.net.
Description:
In Previous article I explained clearly about how to reador write connectionstrings from web.config file in asp.net. Now I will explain how to write appsettings in web.config file and how to read appsettings from web.config file using asp.net.
In Previous article I explained clearly about how to reador write connectionstrings from web.config file in asp.net. Now I will explain how to write appsettings in web.config file and how to read appsettings from web.config file using asp.net.
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
<appSettings>
<add key="yourappsettingsstringName" value=" yourappsettingsvalue" />
</appSettings>
|
Example
of declaring appSettings
in web.config file like this
<appSettings>
<add key="dbconnection" value="Data SOurce=GohilRajni;Initial Catalog=MysampleDB;
Integrated Security=true"/>
</appSettings>
|
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="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Read or Write
appsetting values from web.config file in asp.net</title>
</head>
<body>
<form id="form1"
runat="server">
<div>
<asp:Label ID="lblText"
runat="server"/>
</div>
</form>
</body>
</html>
|
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;
}
|
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.
0 comments :
Post a Comment