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
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
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.
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();