title: ASP.NET C# Static Variables are global?
body: <p>Today I released a small asp.net beta web application which allows internal staff to modify some product information. We started running into issues where users were overwriting each others product information... even though each staff member was editing a totally different row (Product).</p>
  
  <p>After some searching on google, I think I know what is going on, its to do with the use of static variables, below is a quick rough example of the problem:</p>
  
  <pre><code>// EditProductGroup.aspx.cs
  public partial class EditProductGroup : System.Web.UI.Page
  {
  
      private static int _groupId = 0;
  
      protected void Page_Load(object sender, EventArgs e)
      {
        _groupId = Convert.ToInt16(Request.QueryString["GroupID"]);
  
       // get existing ProductGroup information from database using 
       //_groupId to find the row and populate textboxes
      }
  
      private void saveProductGroupData()
      {
        // when user hits 'save changes' button, update row 
        //where the primary key column 'ProductGroupID' matches _groupId in the table
      }
  }
  </code></pre>
  
  <p>So, according to my research, a static variable actually exists to the application as a whole, that means that if multiple users are using the application they will effectively all be reading the same 'value' for '_groupId' and in some cases actually setting it to a different value causing another users 'instance' of the page to save data to the wrong row (ProductGroupId).</p>
  
  <p>My intention was that the static variable is isolated from other users, and should not intefere - each user has their own instance of the page thus their own instance of the '_groupId' variable.</p>
  
  <p>Luckily it was all taking place on a dev/staging database not the live db. I'm not sure whether I just need to drop off the 'static' keyword to stop the variable being set/read by everyone.</p>
  
  <p>Any thoughts? Thanks</p>