Modify Database connection string of web.config..?

Go To StackoverFlow.com

1

Good day! I would like to ask, if you know how to modify the web.config.. Its located in a different folder.. I tried using this approach, Unfortunately i doesn't work..

// set Path to your config file
System.Configuration.ConfigurationFileMap fileMap = new ConfigurationFileMap(sWebConfig);
// open web.config 
System.Configuration.Configuration configuration = System.Configuration.ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
// display message 
Console.WriteLine("Updating wizardConnection string.. Please wait for a few minutes..");
// fetch WizardConnection database connection string 
var Wizardsection = (ConnectionStringsSection)configuration.GetSection("WizardConnection");
// assign new value to wizardConnection.. Please make sure you have the correct database server. Just update server location, if need
Wizardsection.ConnectionStrings["WizardConnection"].ConnectionString = string.Format(@"server={0};database={1};integrated security=SSPI", sDatabaseServer, sDatabase);
configuration.Save();

Hope to hear from you soon..

Regards,

Link

2012-04-04 07:40
by Link
I don't think the web.config is writable from the application - Thorsten Dittmar 2012-04-04 07:46


0

Instead of updating web.config, you may use different web.config files for different build configurations. For instance, you may setup a new build configuration namely "Staging" and configure it to use a modified config file. Have a look at Web.Config transformation

2012-04-04 07:52
by daryal


0

Actually what you are doing will change the config file which is copied to folder that contains binary(debug, release or any custom compiler configuration). That is sufficient to change the connection settings of the application. But, if you want to modify the web.config file, it will be nothing but modifying an external file, for which you will need File opertaions.

2012-04-04 07:54
by Prakash


0

You should use webconfigurationmanager to open your webconfig.

Try this

        var config = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);
        config.ConnectionStrings.ConnectionStrings["ConnectString"].ConnectionString = string.Format(@"server={0};database={1};integrated security=SSPI", sDatabaseServer, sDatabase);

        config.Save();
2012-04-04 08:09
by shenhengbin
Ads