Articles → .NET → Consequence Of Changing Web.Config At Runtime

Consequence Of Changing Web.Config At Runtime

Software Requirement

Prerequisite Knowledge

  1. How to create a project using visual studio?
  2. Basics about controls and their events.
  3. Basics about state objects like session, viewstate etc.

Steps Of Execution

  1. Create a new project
  2. Add controls on page

Create A New Project

Picture shows the sample project in visual studio
Click to Enlarge

Add Controls On Page

  1. Label – To display the value of session variable on click of button with the caption ‘Get Session Value’
  2. Get session value button – When clicked get the value of session variable and display the value in the label.
  3. Change web.config button – Change the value of connection string with key ‘ApplicationServices’ in web.config file.
  4. Set session value button - Set the value of session variable to some hard coded value (4 in this current example).


<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";
//set session value
protected void Button3_Click(object sender, EventArgs e) {
  Session["ID"] = 4;


Picture displays the effect of changing the web.config file on session variable
Click to Enlarge

Posted By  -  Karan Gupta
Posted On  -  Tuesday, October 1, 2013


Your Email Id  
Query/FeedbackCharacters remaining 250