Click here to Skip to main content
12,824,787 members (42,392 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#3.0 C# ASP.NET
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 27-Jun-11 23:18pm
Updated 27-Jun-11 23:43pm
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Use NameSpace

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
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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[^].
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

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:
/// <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
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[^]
[^]
  Permalink  
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)

    Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.170308.1 | Last Updated 7 Oct 2015
Copyright © CodeProject, 1999-2017
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