Modify the web.config's value at runtime

Posted by Lakhangarg under ASP.NET category on | Views : 2672
This code snippets will describe the way with which we can change the value of key present in the appsetting section of the web.config.

Suppose you have a key named Price as:
<add key="Price" value="5" />

and you want to change this value dynamically.

Code to Change the above Value:
Namespace needs to added for this:
using System.Configuration;
using System.Web.Configuration;

Configuration myconf = WebConfigurationManager.OpenWebConfiguration("~");
myconf.AppSettings.Settings["Price"].Value = txtPrice.Text.Trim();

In the above code txtPrice.Text contains the new value for price.

Thanks & Regards
Lakhan Pal Garg

Comments or Responses

Login to post response