My requirement is, on the first time loading the application i want to fetch information about connection string from a database and next time onward's connect using the new connection string (which is fetched from the db)
is this possible in .net.? The connection string is written in web.config file
do I need to restart IIS whenever the web.config changes?
I tried this:
public ActionResult Index()
{
UpdateSetting("test", "123");
UpdateConnectionString("testcon", "12345");
return View();
}
/// <summary>
/// Updates the setting.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void UpdateSetting(string key, string value)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
if (config.AppSettings.Settings[key] == null)
{
config.AppSettings.Settings.Add(key, value);
}
else
{
config.AppSettings.Settings[key].Value = value;
}
config.Save();
ConfigurationManager.RefreshSection("appSettings");
}
/// <summary>
/// Updates the connection string.
/// </summary>
/// <param name="key">The key.</param>
/// <param name="value">The value.</param>
public void UpdateConnectionString(string key, string value)
{
Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
if (config.ConnectionStrings.ConnectionStrings[key] == null)
{
config.ConnectionStrings.ConnectionStrings.Add(new ConnectionStringSettings(key, value));
}
else
{
config.ConnectionStrings.ConnectionStrings[key].ConnectionString = value;
}
config.Save();
ConfigurationManager.RefreshSection("connectionStrings");
}
This function will add a new connection string but is there any other method so that i don't want to restart the application?