Solved

Change the connection string in the web.config programmatically using c#

Posted on 2012-03-23
1
436 Views
Last Modified: 2012-03-23
Hi,

I have been searching for a way to change my connection string at runtime in a windows c# .net application. I have used code like the following...

Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                config.ConnectionStrings.ConnectionStrings["MyConnection"].ConnectionString = PassMan.CurrentConnectionString;
                config.Save(ConfigurationSaveMode.Modified, true);
                ConfigurationManager.RefreshSection("connectionStrings");

But nothing seems to be happening to the web.config file?

Your help would be appreciated.
0
Comment
Question by:jazz__man
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 37758166
The configuration files should be read-only.  What do you need to change dynamically?  If you are working with multiple environments, you should be able to use a different web.config for each environment.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question