I want to configure my web config connection string in my aspx page. That mean I want to change my web.config file dynamically. User can change the connection string any time without touching the server. For that the below asp.net c# code will help you to change connection string , appsetting information and ect.. in dynamically.
private void SetConfigSettings()
{
try
{
// App setting settings
System.Configuration.Configuration config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
//APPsetting value change
System.Configuration.KeyValueConfigurationElement setting = config.AppSettings.Settings["SupportFormat"];
if (null != setting)
config.AppSettings.Settings["SupportFormat"].Value = txtFormat.Text.Trim();
//APPsetting value change
System.Configuration.KeyValueConfigurationElement Pathsetting = config.AppSettings.Settings["Path"];
if (null != Pathsetting)
config.AppSettings.Settings["Path"].Value = txtPath.Text.Trim();
//Connection string changes
string newConnectionString = "Data Source=" + txtServer.Text + ";Initial Catalog='" + txtDatabaseName.Text.Replace("'", "") + "';User ID=" + txtUserName.Text + ";Password=" + txtPassword.Text + ";Persist Security Info=True;";
//Configuration openWebConfiguration = WebConfigurationManager.OpenWebConfiguration("~");
ConnectionStringsSection sections = config.GetSection("connectionStrings") as ConnectionStringsSection;
if (sections != null)
{
sections.ConnectionStrings["ConString"].ConnectionString = newConnectionString;
ConfigurationManager.RefreshSection("ConString");
}
config.Save();
lblMSG.Text = "Updated successfully!!!";
lblMSG.ForeColor = Color.Green;
}
catch (System.Exception exc)
{
lblMSG.Text = exc.Message;
lblMSG.ForeColor = Color.Red;
}
}
Best ASP.NET Hosting Recommendation
ASPHostPortal.com provides its customers with Plesk Panel, one of the most popular and stable control panels for Windows hosting, as free. You could also see the latest .NET framework, a crazy amount of functionality as well as Large disk space, bandwidth, MSSQL databases and more. All those give people the convenience to build up a powerful site in Windows server. ASPHostPortal.com offers ASP.NET hosting starts from $1/month only. They also guarantees 30 days money back and guarantee 99.9% uptime. If you need a reliable affordable ASP.NET Hosting, ASPHostPortal.com should be your best choice.