Articles → .NET → Consequence Of Changing Web.Config At Runtime
Consequence Of Changing Web.Config At Runtime
Software Requirement
Prerequisite Knowledge
- How to create a project using visual studio?
- Basics about asp.net controls and their events.
- Basics about state objects like session, viewstate etc.
Steps Of Execution
- Create a new project
- Add controls on page
Create A New Project
Click to Enlarge
Add Controls On Page
- Label – To display the value of session variable on click of button with the caption ‘Get Session Value’
- Get session value button – When clicked get the value of session variable and display the value in the label.
- Change web.config button – Change the value of connection string with key ‘ApplicationServices’ in web.config file.
- Set session value button - Set the value of session variable to some hard coded value (4 in this current example).
Code
<asp:Button ID="Button1" runat="server" Text="Get session value" OnClick="Button1_Click"/>
<asp:Button ID="Button2" runat="server" Text="Change web.config" OnClick="Button2_Click" />
<asp:Button ID="Button3" runat="server" Text="Set session value" OnClick="Button3_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
//Get session value
protected void Button1_Click(object sender, EventArgs e) {
Label1.Text = "Current Session value is : " + Session["ID"];
}
//Change config value
protected void Button2_Click(object sender, EventArgs e) {
Configuration conf = WebConfigurationManager.OpenWebConfiguration("~");
conf.ConnectionStrings.ConnectionStrings["ApplicationServices"].ConnectionString = "Test1";
conf.Save();
}
//set session value
protected void Button3_Click(object sender, EventArgs e) {
Session["ID"] = 4;
}
Output
Click to Enlarge