Hello Friends,
One of my close friend asking me to change or modify the web.config file on fly or at runtime because his boss ask to have a page in their backoffice, where they can change the connection string of the database, or changing the session Timeout.
so here is the solution where you can get web.config files data and modify it and update web.config file.
first of all
keep the default settings and run the Default.aspx page. When you compile and run, a dialog box will be prompted to you concerning the web.config file, asking whether you want to modify the web.config to enable debugging, or to run the website without debugging (The site will be be opened using a temporary port created by visual studio). Make sure the first option is selected and click ok.
IF you want to access <connectionStrings> section then follow below code.
let take one example where you have this kind of text in web .config file
<connectionStrings>
<add name=”yourdatabasename” connectionString=”your connection string place here”/>
</connectionStrings>
In Web .Config file we have a section name as <appSettings/> where we can specify the our system setting variables like below.
<add key=”myKey” value=”myValue” />
First if you want to get these values at runtime in any page.
‘ Code Starts Here
If Page.IsPostBack = False Then
txtConnectionString.Text = System.Web.Configuration.WebConfigurationManager.ConnectionStrings(”yourdatabasename“).ConnectionString
txtmyKey.Text = System.Web.Configuration.WebConfigurationManager.AppSettings(”myKey”)
End IF
‘ Code Ends Here
Now that we’ve read those variables and passed them to our textboxes, we will change their values and click the “Submit” button to save the changes. To do so, double click on the Submit button and add the following code:
‘ Code Starts Here
Dim myConfiguration As Configuration = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(”~”)
myConfiguration.ConnectionStrings.ConnectionStrings(”yourdatabasename”).ConnectionString = txtConnectionString.Text
myConfiguration.AppSettings.Settings.Item(”myKey”).Value = txtmyKey.Text
myConfiguration.Save()
‘ Code Ends Here
One this make sure that whenever you change your web.config file at run time after changed your web.config file your all current session will be expire.
Enjoy Friends
Tags: Change web.config file at runtime, Change web.config file On Fly, Modify the web.config file at runtime, Modify the web.config file On Fly
Hello ! I am Arjun Jadeja a Software Engineer by Profession. You can contribute and I will distribute your ideas through this site. Thanks and Enjoy
Leave a reply