Sunday, 15 May 2016

Reading appSettings values from web.config file in ASP.Net with C#

In this article, we are going to learn reading appSettings values from web.config file in asp.net. In the previous article, I have explained, reading connectionString from web.config file in asp.net. This article is similar to previous one.

In Web.Config File

Below is the structure of web.config file. In the appSettings, we can write application related settings like database connection strings, Document file location, Email setting,...etc using add key and value.

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="UploadFilePath" value="D:\Docs"/>
    <add key="FromMail" value="from@gmail.com"/>
    <add key="ToMail" value="to@gmail.com"/>
  </appSettings>
</configuration>

And also, we need to add the System.Configuration dll to project. Below is step to add System.Configuration dll.

Step 1: Write click on references and click on add reference as shown below.

add System.Configuration reference1

Step 2: Now the new window will open as show below. Go to Framework scroll down to System.Configuration and check to add and click on OK.

add System.Configuration reference2

Step 3: Rebuild your application.

Step 4: Now, go to your Code-Behind file and include the below Namespace.

using System.Configuration;

Step 5: Below is the code snippet to read the appSettings values from web.config file using c#.

protected void Page_Load(object sender, EventArgs e)
{ 
string UploadFilePath = ConfigurationManager.AppSettings["UploadFilePath"].ToString();
string FromMail = ConfigurationManager.AppSettings["FromMail"].ToString();
string ToMail = ConfigurationManager.AppSettings["ToMail"].ToString();
}
Share:

0 comments:

Post a Comment

Email Subscription

Subscribe to our newsletter to get the latest articles directly into your inbox

Blog Archive

BUY FROM AMAZON