2

I wanted to use some data from one page to another. General solution is to use Session (most of recommends). My boss don't like sessions and he suggested me to do same work by using C# Property in common class as below

public static long parentId { get; set; }

and set it one one page as

Common.parentId = "any value";

and use it on other page like

string anyVariable = Common.parentId

and it worked. We get rid of session expiration as well. But why most of people recommend session. Is property another state management thing?

Imad
  • 7,126
  • 12
  • 55
  • 112
  • 4
    If you don't care about the values shared being across all sessions you can use static properties. – Sriram Sakthivel Nov 17 '14 at 08:03
  • 6
    Is it a `static` variable in the `Common class`? If you you are doing it wrong because all the user will get the same value. It's better to use `Session` or [pass the values from one page to another](http://stackoverflow.com/questions/14956027/how-to-pass-values-across-the-pages-in-asp-net-insted-of-session#14956100) while navigation which is very difficult to control. – शेखर Nov 17 '14 at 08:03
  • If it's temporary you can use the AppCache to store the object, plenty of docs on how to use it, you can store the data with an ID (maybe the user or a page reference) and then pull it out on the page. – starlight54 Nov 17 '14 at 08:19
  • @Șhȇkhaṝ sharing a variable across multiple user means? If I open my application from two browser and running set code in one and get code from another, will I get the same value that is set by other browser? – Imad Nov 17 '14 at 09:04
  • @Imad yes if value is all ready set then old value will be lost. the previous user will see the update value. – शेखर Nov 17 '14 at 09:42

1 Answers1

0

If you are going to store some data using simple static property you must understand that it will be shared among all your users. Sessions are not for this. But I don't see any reasons not to use sessions if you want to store user data somewhere.

In my project it is very convenient, especially when we use SQL-server to store sessions - we can update our website without any losing users' sessions data. You can check all possible ways to hanle sessions for example here http://msdn.microsoft.com/en-us/library/75x4ha6s(v=vs.100).aspx

Viktor Kireev
  • 1,200
  • 1
  • 8
  • 17