tags:

views:

82

answers:

4

Hello All,

How can I add and read value from web.config

Thanks in Advance

+2  A: 

Assuming the key is contained inside the <appSettings> node:

ConfigurationSettings.AppSettings["theKey"];

As for "writing" - put simply, dont.

The web.config is not designed for that, if you're going to be changing a value constantly, put it in a static helper class.

RPM1984
+2  A: 

I would suggest you to don't modify web.config from your, because every time when change, it will restart your application.

However you can read web.config using System.Configuration.ConfigurationManager.AppSettings

Muhammad Akhtar
Thank You Mr.Muhammed , so what you advice me to do to save a variable in a public place that can be changed without restating the web application ? Thanks in Advance
Amira Elsayed
You can store such variables in an encrypted XML file.
vamyip
yes, XML file is the better idea. Or you can store it in DB and add in application_start (Global.asax), put it in application variable and use these in application. these variable assign only once in the application and if your application restart, these will assigned again.
Muhammad Akhtar
Thank you very much Mr.Vamyip and Mr.Muhammed for your help
Amira Elsayed
+1  A: 

Ryan Farley has a great post about this in his blog, including all the reasons why not to write back into web.config files: Writing to Your .NET Application's Config File

CD
+8  A: 

Hello ,

Suppose In web config You have variable like that

<appSettings>

     <add key="ClientId" value="127605460617602"/>

     <add key ="RedirectUrl" value="http://localhost:49548/Redirect.aspx"/&gt;
  </appSettings>

In aspx page you can use like that ..........

1st you have to add namespace

using System.Configuration;

Then you can use

string Clientid=ConfigurationManager.AppSettings["ClientId"].ToString();
string Redircturl=ConfigurationManager.AppSettings["RedirectUrl"].ToString();

Hope it works for you.

PrateekSaluja