Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post May 10, 2010 03:33 PM by ELiva
Dec 23, 2008 05:45 AM|LINK
I have two connection strings stored in the web.config I use, one for dev/debug, and the other to test on my production server. Everytime I publish my work to the production server I have to remember to edit my web.config and comment out my dev/debug connectionstring
and uncomment my production connection string.
Now I have a debug variable that I set throughout the project if it is being hosted on my development machine (looks for hostname). Is it possible to programmatically edit the web.config based on the computer hostname like I do with other portions of the
project? Specifically the <connectionString> part of the web.config.
Dec 23, 2008 06:56 AM|LINK
use this To edit
As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
myConfiguration.ConnectionStrings.ConnectionStrings("myDatabaseName").ConnectionString = txtConnectionString.Text
myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text
Dec 24, 2008 03:13 PM|LINK
I can't seem to get that to work. I already have a connection string setup in my web.config for the production server, however when I try to change it because its running on my development machine it doesn't change and I can't login to the database
protected void setConfigurationSettings(string hostname)
string strDevConnection = @"Data Source=DEVELOPMENT\SQLEXPRESS;Initial Catalog=sqlDB;Persist Security Info=True;User ID= ;Password= ";
string strLiveConnection = @"Data Source=PRODUCTION\SQLEXPRESS;Initial Catalog=sqlDB;User id= ;password= ";
Configuration myWebConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
if (hostname == "devPC")
myWebConfig.ConnectionStrings.ConnectionStrings["DBConnectionString"].ConnectionString = strDevConnection; //DBConnectionString is the name of the current connectionstring in the web.config
myWebConfig.ConnectionStrings.ConnectionStrings["DBConnectionString"].ConnectionString = strLiveConnection;
Dec 24, 2008 03:19 PM|LINK
Just found the mistake... I forgot to put myWebConfig.Save(); at the end :)
Dec 24, 2008 03:23 PM|LINK
Its working I have tested!!
What u missed is to save
Dec 24, 2008 03:30 PM|LINK
Yes I figured it out immediately after posting again. Thank you very much!!!
Mar 25, 2010 08:26 PM|LINK
this maybe a better solution:
May 10, 2010 03:33 PM|LINK
I know this is an old post but thought it might still be helpful to someone.
I use code to get the machine name.
I then look up that machine name in my appSettings section of the web.config
Once it finds the machine name in the web.config it knows the prefix (DEV, UAT or PROD) to use to go after the correct connection string.
I now never have to change my web.config between UAT and Prod deploy. The only time I need to change it is if the application is placed on a different machine (very rare) in which case all I need to do is change the name of the machine in the web.config.
I can provide code samples to anyone interested.