-->

Friday, June 19, 2015

How to access application setting tag of web.config file in asp.net

How to access application setting tag of web.config file in asp.net

Today i am writing ado.net code in code behind file, On that time i notice that if i take my connection string in the web.config file then we can access it easily. But how? So i have search in msdn library to access the web.config file then i found ConfigurationManager class which is available in System.Configuration Namespace. Through this class we can get all elements of web.config file. Lets take a simple example.




Web.config file

<appSettings>
    <add key="jacob" value="My Name"/>
   
  </appSettings>

Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Readwebxmlfile.aspx.cs" Inherits="Readwebxmlfile" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    </div>
    <asp:Button ID="Button1" runat="server" Height="57px" OnClick="Button1_Click" Text="Get Data from Application Setting" Width="232px" />
    </form>
</body>
</html>

Code Behind Code
using System.Configuration;
 protected void Button1_Click(object sender, EventArgs e)
    {
        String getname = ConfigurationManager.AppSettings["jacob"].ToString();
        Label1.Text = getname;
    }

Here we access AppSettings By the configurationManager class. You can access all property or elements from web.config file by using this class. Now the simple syntax to access them.
ConfigurationManager.elementName

Read other related articles

Also read other articles

© Copyright 2013 Computer Programming | All Right Reserved