Friday, February 24, 2012

Dynamic Connection String Problem

I need to be able to deploy my updated website to many customers on a monthly basis and dont want to be mucking around changing the connection strings each time. Some of my web servers have multiple copies of my site and DB so each website will need a different connection string.

The simplest method I could come up with is to use the Application Name field in IIS as it doesn't get overwritten by Visual Studio when I deploy the site.

I am trying to write some code to dynamically change the connection string in the web config but cannot find any way of reading the Application Name field in IIS to use in altering the connection string. I'm using the Global.asax file to change the connection string before the DB gets called.

I had tried embedding the DB in the website folder but it would overwrite the customers database.

Hi Matthew,

The only way that comes to me is that we can write a small tool that parses the web.config file as xml, and change the connection string accordingly.

HTH. If this does not answer you question, please feel free to mark it as Not Answered and post your reply. Thanks!

|||

I searched and searched and eventually found this snippet but it the last line doesn't work for me. For other people it has worked and I'm not sure what is wrong with my setup:

' Create a connectionn string element and add it to the connection strings section.Sub ChangeConnectionString(ByVal cs_nameAs String,ByVal cs_valueAs String)Try Dim webConfigAs System.Configuration.Configuration = WebConfigurationManager.OpenWebConfiguration("~")Dim dbConnStringAs ConnectionStringsSection = webConfig.ConnectionStrings'// To change existing connectionstring dbConnString.ConnectionStrings(cs_name).ConnectionString = cs_value webConfig.Save()Catch exAs ExceptionEnd Try End Sub
|||

Hi Matthew,

Could you show us the exception message?

|||

The exception is:

{System.Configuration.ConfigurationErrorsException}

An error occurred loading a configuration file: Access to the path 'C:\inetpub\wwwroot\flo_dev\6egcsvyu.tmp' is denied. (C:\inetpub\wwwroot\flo_dev\web.config)

Hope that spreads some light on what it might be.

|||

Hi Matthew,

If you're using an ASP.NET app to do this, please check if the account ASPNET do have permission to write to that file.

|||

I will check the rights of the ASPNET user. If the ASPNET user has rights to modify that file wont that allow anyone to alter the file? Or just server-side code?

No comments:

Post a Comment