title: Static variable changing values in ASP.NET C#
body: <p>I have a problem with a static variable in ASP.NET using C#. I'm declaring the variable in a webform.</p>
  
  <pre><code>public partial class Logueado_Movimientos : System.Web.UI.Page
  {
     static List&lt;ExchangeItems&gt; theList;
  
     protected void Page_Load(object sender, EventArgs e)
     {
        if (IsPostback) return;
        theList = GetListValues();
     }
  }
  </code></pre>
  
  <p>So far, so good. We tested the site, no problems were found, deployed it... but in production environment something weird has happened. This site is used daily all day long and just twice, a situation has happened.</p>
  
  <p>ExchangeItems has an ID property, which returns the id from the database for each item to be exchanged. The GetListValues() method is called only once when the page loads. After that, the user can select items to be exchanged by clicking on a checkbox in the GridView, make further validations and after all that, there's a "Print and Save" button, which prints to a PDF using iTextSharp and sends the status back to the database.</p>
  
  <p>After all the validations, the item has been changed twice in production. For example, item 180 is the one that is being exchanged, but when the document is printed and saved, it turns out that item 103 is the one processed. All previous validations have the ID as 180. Item 103 was not even in the list to begin with.</p>
  
  <p>Now, checking the database (SQL Server) we found that item 103 was saved 10 minutes after item 180. We use the GetDate() function to store the date and time. Furthermore, they were assigned to different customers by two different users.</p>
  
  <p>It is possible that the user takes those 10 minutes to process the request, sometimes they are on the phone with the customer. That means that user1 is working with item 180 and user2 is working with item 103, both using the same module/webform. Since the variable is static, is it possible that both users are affecting each other's information? I'm declaring it now as "private static", just out of paranoia, but is there anything else I'm missing?</p>
  
  <p>Note: the variable is static because the postback losses it's value if not declared so. It is not in the Session variable, because it is only used in that module/webform, nowhere else.</p>