Modify your web.config from an ASP.NET application

I need to change the database connection string in the web.config file, but havent figured out how to do this. Does anyone have any experience with this?

UPDATE
Sorry, it is not clear above, I want to be able to change the connection string from a web application after it is deployed.

+4
source share
2 answers
Configuration myConfiguration = WebConfigurationManager.OpenWebConfiguration("~"); //appSettings configuration myConfiguration.AppSettings.Settings["xxx"].Value = "yyy"; //database connections configuration myConfiguration.ConnectionStrings.ConnectionStrings["xxxconnection"].ConnectionString = "yyy"; myConfiguration.Save(); 

http://msdn.microsoft.com/en-us/library/system.configuration.configuration.connectionstrings.aspx

Edit:

http://msdn.microsoft.com/en-us/library/system.configuration.connectionstringssection.aspx

In addition, as pointed out by others, you need to set the correct permissions, but sometimes using shared hosting (from experience), they ask you for the username and password of your account, and as soon as you enter it, your web.config will change. So try it, and if that doesn't work, and you don’t have permission to configure permissions, I’m afraid you need to think about something else.

Good luck

+2
source

Use the WebConfigurationManager class, as shown here .

Since this is very important information, you must set the correct permissions as described on this site (link provided by David Stratton).

+1
source

All Articles