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.
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
}
else
{
myWebConfig.ConnectionStrings.ConnectionStrings["DBConnectionString"].ConnectionString = strLiveConnection;
}
}
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.
drpcken
Member
454 Points
342 Posts
change web.config at runtime
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.
Thanks!
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: change web.config at runtime
Dec 23, 2008 06:56 AM|LINK
Hi
use this To edit
Dim myConfiguration As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~")
myConfiguration.ConnectionStrings.ConnectionStrings("myDatabaseName").ConnectionString = txtConnectionString.Text
myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text
myConfiguration.Save()
Contact me
drpcken
Member
454 Points
342 Posts
Re: change web.config at runtime
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 } else { myWebConfig.ConnectionStrings.ConnectionStrings["DBConnectionString"].ConnectionString = strLiveConnection; } }drpcken
Member
454 Points
342 Posts
Re: change web.config at runtime
Dec 24, 2008 03:19 PM|LINK
Just found the mistake... I forgot to put myWebConfig.Save(); at the end :)
Thanks!!!
mudassarkhan
All-Star
78956 Points
13402 Posts
MVP
Re: change web.config at runtime
Dec 24, 2008 03:23 PM|LINK
Its working I have tested!!
What u missed is to save
myWebConfig .Save()
Contact me
drpcken
Member
454 Points
342 Posts
Re: change web.config at runtime
Dec 24, 2008 03:30 PM|LINK
Yes I figured it out immediately after posting again. Thank you very much!!!
wtwynn
Member
6 Points
3 Posts
Re: change web.config at runtime
Mar 25, 2010 08:26 PM|LINK
this maybe a better solution:
http://forums.asp.net/t/1521221.aspx
ELiva
Member
29 Points
19 Posts
Re: change web.config at runtime
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.