• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 453
  • Last Modified:

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.
0
jazz__man
Asked:
jazz__man
1 Solution
 
Bob LearnedCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now