53

Does static variables retain their values across user sessions?

I have a ASP.NET web application where I have two buttons. One for setting the static variable value, another for Showing the static variable value.

namespace WebApplication1
{   

public partial class WebForm1 : System.Web.UI.Page
{
    public static int customerID;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void ButtonSetCustomerID_Click(object sender, EventArgs e)
    {
        customerID = Convert.ToInt32(TextBox1.Text);
    }

    protected void ButtonGetCustomerID_Click(object sender, EventArgs e)
    {
        Label1.Text = Convert.ToString(customerID);
    }
}

}

While this works in single-user environment, What happens if there are 2 users simultaneously logged in from two computers, User 1 sets the value as 100, then User 2 sets the value as 200. after that user 1 invokes the Get Value button. What will he see as the value?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Manas Saha
  • 1,477
  • 9
  • 29
  • 44

4 Answers4

79

Does static variables retain their values across user sessions?

Yes, that's why you should be VERY careful when you use static variables in a web app. You will run in concurrency issues as more than one thread servicing a request can modify the value of the variable.

While this works in single-user environment, What happens if there are 2 users simultaneously logged in from two computers, User 1 sets the value as 100, then User 2 sets the value as 200. after that user 1 invokes the Get Value button. What will he see as the value?

The user will see 200 afterwards.

Icarus
  • 63,293
  • 14
  • 100
  • 115
25

Static Variables Scope is Application Level.

If you store something in Static variables, definitely your doing something wrong.

If one user saves the data(In Static variable), Same time another user access same page then he will get the same data(First User saved).

So better you can store the values in **Sessions**.
andy
  • 5,979
  • 2
  • 27
  • 49
  • can Session variables be configured to use memory? I have a situation where an api consumer doesn't store or retrieve cookies and I don't want a database hit, its for something happening multiple times a second. – John Mott Feb 09 '18 at 20:28
  • Does this matter if all users need to see the exact same data anyway and the only consideration is seeing whatever is most recent, and the users can't change the data themselves? – user3776749 Jan 28 '20 at 01:16
2

This would work for you (keep in mind, you need to handle null values/-1):

public static int customerID
{
    get { return session["customerID"] == null? -1 : (int)session["customerID"]; }
    set { session["customerID"] = value; }
}
  • 6
    You can't access the session variables directly as "Session" from a static get or set accessor. You need to use HttpContext.Current.Session instead. – Stan Shaw Oct 29 '15 at 19:20
  • This is very dangerous, for concurrent user maybe return session["customerID"] for other user, agree? – Mohsen Najafzadeh Jun 29 '20 at 11:28
0

Do not use static for the property then it works:

public int customerID
{
    get { return Session["customerID"] == null? -1 : (int)Session["customerID"]; }
    set { Session["customerID"] = value; }
}
heinz
  • 31
  • 2