Solved

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

Posted on 2012-03-23
1
432 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Icons and Colors for Terms 3 24
Why Does This Page Not Show Up? 4 22
Tool Box 2 34
Help with LINQ and XML 10 26
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.

911 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now