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 read or 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 read or 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=SureshDasari;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;
}
|
VB.NET
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 Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
5 comments :
I tried over and over times again, but I still can't write appsettings in web.config file. Where the problem is?
can you please tell how to change connection string using C# with user interface
Thanks dude !
About this article, it shows how to read, but never how to write. Change the article's title to make it less frustrating one.
string colorMinsFourty = "Black";
XElement WebIntervalColorChangingElements = XElement.Load(Server.MapPath("~/web.config"));
var eventColorFourty = WebIntervalColorChangingElements.Element("appSettings").Elements("add").Where(w => w.FirstAttribute.Value == "FourtyMinsColor").Select(s => s.FirstAttribute.NextAttribute).FirstOrDefault();
eventColorFourty.Value = colorMinsFourty;
WebIntervalColorChangingElements.Save(Server.MapPath("~/web.config"));
Note: Only a member of this blog may post a comment.