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

Today i am writing 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

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

Source Code

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

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
    <form id="form1" runat="server">
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
    <asp:Button ID="Button1" runat="server" Height="57px" OnClick="Button1_Click" Text="Get Data from Application Setting" Width="232px" />

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.


Post a Comment