Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7498971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:39:24+00:00 2026-05-29T19:39:24+00:00

I have a problem with programmatically change connection string in the app.config file. In

  • 0

I have a problem with programmatically change connection string in the app.config file.
In overriden OnStart method I want to change app.config file(only connection string)
At this moment I have this:

 private static void ChangeConnectionString(string newDatasource)
    {
        try
        {
            XDocument doc = XDocument.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            var query = from p in doc.Descendants("connectionStrings").Descendants()
                         select p;
            foreach (var child in query)
            {
                foreach (var atr in child.Attributes())
                {
                    if (atr.Name.LocalName == "name" && atr.Value == "UniversityEntities" &&
                        atr.NextAttribute != null && atr.NextAttribute.Name == "connectionString")
                    {
                        SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
                        connectionStringBuilder.DataSource = newDatasource;
                        connectionStringBuilder.InitialCatalog = "UniversityDB";
                        connectionStringBuilder.IntegratedSecurity = true;

                        EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder(atr.NextAttribute.Value);
                        entityBuilder.ProviderConnectionString = connectionStringBuilder.ToString();
                        atr.NextAttribute.Value = entityBuilder.ToString();

                    }
                }

            }
            doc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile);
            ConfigurationManager.RefreshSection("connectionString");
        }
        catch (Exception x)
        {
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T19:39:26+00:00Added an answer on May 29, 2026 at 7:39 pm

    if you want to create a UI to modify the config you can do that. but it wouldn’t happen within the application itself.

    1st you have the service/app that will be affected.
    2nd you have another app that will change the file.

    when the admin clicks “change connection string” from the 2nd app, the 2nd app will stop the 1st service/app. change the connection string. start the 1st service/app.

    so from the perspective of the 1st app it won’t have to think about hacking together a connection string.

    ————————————–

    an alternative is to remove the connection string for the config file altogether and simply make the connection string a required parameter when the application starts. so if it’s a server it might look like this

    net start [name of service] “the connection string…”

    from the 2nd UI you could just overwrite a bat/ps script that starts/stops the service. this way you don’t have to hack through the xml of the app.config file.

    ————————————–

    one last comment. remove the try/catch, it’s completely useless and you need to know if there are problems when changing the file.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm developing an iphone app and want to have the option to programmatically change
I'd like to set a connection string programmatically, with absolutely no change to any
I have problem with return statment >.< I want to store all magazine names
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE
I have problem with fancybox. I want to write a function that will run
I have problem with cakephp's Session->write method. If I set a value like $_SESSION['..']
I have a WCF Windows service with an endpoint specified in the config file
Ok, my problem is, I have got n-TextViews and they will be added programmatically
I occasionally want to change an NSView's dimensions programmaticaly. To do this, it is
I have read a ton about this problem, but I cannot figure it out.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.