.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to modify web.config file dynamically?

Posted By: priya     Posted Date: December 23, 2009    Points:2   Category :ASP.Net
Hi,

How to modify web.config file dynamically? I am using asp.net 3.5 and i need to modify connection sting dynamically at run time depending upon some condition.

I am using C# as code behind.

Thanks in advance
Priya

Responses
Author: nishithraj             
Posted Date: December 24, 2009     Points: 5   

you can modify the key dynamically as follows

Configuration webConfigApp = WebConfigurationManager.OpenWebConfiguration("~");
webConfigApp.AppSettings.Settings["Quantity"].Value = "10";
webConfigApp.Save();

Here Quantity is a key value in the Web.config file

Author: dinesh gouttam             
Posted Date: December 24, 2009     Points: 5   

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
config.AppSettings.Settings.Add("MyKey","MyValue");

config.Save(ConfigurationSaveMode.Modified);

Author: nishithraj             
Posted Date: December 28, 2009     Points: 5   

Priya,

You can also try the following too..
http://www.dotnetspark.com/kb/1205-update-information-to-webconfig-run-time.aspx

Author: Lalij Mer             
Posted Date: January 05, 2010     Points: 5   

First lets read existing configuration and value of the settings element.
protected void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["MyValue"];

if (null != setting)
{
// lets do something with the value - displaying it in a textbox is an ides
textboxValue.Text = setting.Value;
}
}
}


We can use web.config file from some other directory if we use its name instead of '~' in WebConfigurationManager.OpenWebConfiguration("~") method.

We've already read the value and displayed it in textbox so we are ready to save updated value (or add new settings element if it hasn't been saved yet).
protected void btnSave_Click(object sender, System.EventArgs e)
{
try
{
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");

System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["MyValue"];

if (null != setting)
{
config.AppSettings.Settings["MyValue"].Value = textboxValue.Text;
}
else
{
config.AppSettings.Settings.Add("MyValue", textboxValue.Text);
}

config.Save();
literalMessage.Text = "New value saved. Application will be restarted automatically.";
}
catch (System.Exception exc)
{
literalMessage.Text = (exc.Message + "<br />" + exc.StackTrace).Replace("\n", "<br />");
}
}

check also this link

http://ramanisandeep.wordpress.com/2009/04/07/programming-the-webconfig-file-using-c/

Please Check my answer if you help....
Thank You...

Lalit.
Author: Sandeep             
Posted Date: May 15, 2010     Points: 5   

Hi all,

Here is a link that provide a good explanation of how to modify we.config and its affect on the application

http://www.a2zmenu.com/CSharp/Modify%20webconfig%20file%20at%20runtime.aspx

Author: Udayan             
Posted Date: July 02, 2010     Points: 5   

Try XML Webpad - http://xmlwebpad.codeplex.com/
It's a lightweight framework to view and edit XML files that can also be seamlessly integrated with any web application. And perfect for modifying web.config dynamically at runtime



Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend