Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: 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 3:15am
Hekmat901.4K
Edited 18-Feb-13 3:31am
v3
Comments
IpsitaMishra at 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 at 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
good
Please Sign up or sign in to vote.

Solution 2

hi dear
 
use below function
 
private bool savesattings(string key, string value)
       {
           try
           {
               System.Configuration.Configuration lobjconfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
               lobjconfig.AppSettings.Settings.Remove(key);
               lobjconfig.AppSettings.Settings.Add(key, value);
               lobjconfig.Save(ConfigurationSaveMode.Modified);
               ConfigurationManager.RefreshSection("appSettings");
               return true;
           }
           catch
           {
               return false;
           }
       }
 
private void btnChange_Click(object sender, EventArgs e)
        {
            savesattings("DemoKey", "MyValue");
        }
  Permalink  
Rate this: bad
good
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 =
ConfigurationManager.ConnectionStrings;
 
// 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;
break;
}
}
return returnValue;
}
 
 
For more details, refer below link
 
http://msdn.microsoft.com/en-us/library/ms254494.aspx[^]
  Permalink  

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

  Print Answers RSS
0 Peter Leow 210
1 Abhinav S 155
2 Mika Wendelius 131
3 Sergey Alexandrovich Kryukov 125
4 TheRealSteveJudge 114
0 Sergey Alexandrovich Kryukov 8,613
1 OriginalGriff 6,536
2 Peter Leow 3,727
3 Zoltán Zörgő 3,456
4 Richard MacCutchan 2,442


Advertise | Privacy | Mobile
Web04 | 2.8.150123.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2015
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