Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET C# Static Variables are global?

Tags:

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).

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:

// 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     } } 

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).

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.

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.

Any thoughts? Thanks

like image 349
Dalbir Singh Avatar asked Oct 13 '09 21:10

Dalbir Singh


1 Answers

Yes, in ASP.NET a static fields lifetime is for the app domain (note that this differs a bit for generic types).

I'd recommend using the server Session for storage of data you want to associate with an instance of a client browser session (user login). i.e.

Session["_groupID"] = Convert.ToInt16(Request.QueryString["GroupID"]); 

you can retrieve it by doing:

short groupID = Convert.ToInt16(Session["_groupID"]); 
like image 102
Adam Markowitz Avatar answered Nov 11 '22 16:11

Adam Markowitz