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

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.
LVL 2
jazz__manAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.