Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,
Suppose I have this connection string in my web.config(Asp.net C#)

<appSettings>
<add key="constring" value="server=Virus-WS05;user id=sa;database=Virus; password=Virus2011"/>
</appSettings>


Now here If admin change the password of the database from virus2011 to virus, then how can admin set it in web.config dynamically for all the users.
(not by downloading the web.config from ftp and then again hosting it as this process sucks).sdf

Thanks

[edit]Code block added, "Ignore HTML..." option disabled - OriginalGriff[/edit]
Posted
Updated 27-Jun-11 22:43pm
v2

Use NameSpace

C#
using System.Configuration;
using System.Web.Configuration;

void ConfigurnewConnectionString(string server, string database, string userid, string password)
    {

        string str = "server=" + server + ";database=" + database + "; User ID=" + userid + "; Password=" + password + "";
        //Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        //str = System.Web.Configuration.WebConfigurationManager.AppSettings["myKey"];
        //myConfiguration.Save();
        System.Configuration.Configuration Config1 = WebConfigurationManager.OpenWebConfiguration("~");
        ConnectionStringsSection conSetting = (ConnectionStringsSection)Config1.GetSection("connectionStrings");
        ConnectionStringSettings StringSettings = new ConnectionStringSettings("conn", "Data Source=" + server + ";Database=" + database + ";User ID=" + userid + ";Password=" + password + ";");
        conSetting.ConnectionStrings.Remove(StringSettings);
        conSetting.ConnectionStrings.Add(StringSettings);
        Config1.Save(ConfigurationSaveMode.Modified);
        //Configuration myConfiguration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("~");
        //myConfiguration.AppSettings.Settings.Item("myKey").Value = txtmyKey.Text;
        //myConfiguration.Save();
    }


Pass servername,databasename,userid,pasword from this function
 
Share this answer
 
v2
Comments
divyansh singh 28-Jun-11 8:21am    
thanks it's works..:)
PunjabiTor 17-Oct-12 16:34pm    
How do we set providerName="System.Data.SqlClient"?
Muthu Vinoth Kumar 14-May-14 7:42am    
Its Work Fine.. But i met one pbm. Everytime it ask to reload the webconfig file.
Muthu Vinoth Kumar 14-May-14 7:45am    
http://s11.postimg.org/8y38xcdkj/Untitled.png
First of all, you are going to need to set up authentication on your system (assuming you haven't done so already), and have the administrator sign in. When the administrator has signed in, they will be able to access an administration page that you will write which will provide a field for changing the password. Your code will then validate that the password against the database - typically by attempting to connect to it, and you will then write the value back into the config file.

Details on how to read the config file (and save) can be found here[^].
 
Share this answer
 
HTML
How to dynamically change connection string in web.config
Most of people wonder how they can dynamically change the contents of web.config file. To simplify things I have created couple of functions through which you can change the contents of web.config file. Here is the code to change the contents of web.config file:
C#
/// <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");
}

then
C#
protected void Page_Load(object sender, EventArgs e)
{
    UpdateSetting("test", "123");
    UpdateConnectionString("testcon", "12345");
}

http://zeeshanumardotnet.blogspot.in/2011/12/how-to-dynamically-change-connection.html[^]
[^]
 
Share this answer
 
v3
Comments
Deepu S Nair 31-Mar-15 8:30am    
Did you notice that this question is nearly 4 years old ?
This will only attract down-voting ... it's not a good idea

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