Click here to Skip to main content
12,896,826 members (58,854 online)
Rate this:
Please Sign up or sign in to vote.
See more: C# C#4.0
I am using app.config file for define connectionstring;
how to edit line of connectionstring code in app.config in runed program?
Posted 18-Feb-13 2:15am
Updated 18-Feb-13 2:31am
IpsitaMishra 18-Feb-13 8:22am
What exactly your requirement is ??? do you want to get different connection string at different server ..or something else....please specify your requirement .....
Devang Vaja 19-Feb-13 0:14am
Ipsita i think he want to change connection string in running program...
Ofcousrse You can change connection string but you have to refresh the page after changing connctionstring....
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

hi dear

use below function

private bool savesattings(string key, string value)
               System.Configuration.Configuration lobjconfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
               lobjconfig.AppSettings.Settings.Add(key, value);
               return true;
               return false;

private void btnChange_Click(object sender, EventArgs e)
            savesattings("DemoKey", "MyValue");
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

// Retrieve a connection string by specifying the providerName.
// Assumes one connection string per provider in the config file.
static string GetConnectionStringByProvider(string providerName)
// Return null on failure.
string returnValue = null;
// Get the collection of connection strings.
ConnectionStringSettingsCollection settings =
// Walk through the collection and return the first
// connection string matching the providerName.
if (settings != null)
foreach (ConnectionStringSettings cs in settings)
if (cs.ProviderName == providerName)
returnValue = cs.ConnectionString;
return returnValue;

For more details, refer below link[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

    Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170424.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100