Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using app.config file for define connectionstring;
how to edit line of connectionstring code in app.config in runed program?
Posted
Updated 18-Feb-13 2:31am
v3
Comments
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....

hi dear

use below function

C#
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;
           }
       }


C#
private void btnChange_Click(object sender, EventArgs e)
        {
            savesattings("DemoKey", "MyValue");
        }
 
Share this answer
 
// 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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900