Tuesday, September 29, 2009

ASP.NET Methods for carrying User data







One of the frequently faced problems by people who have started work in asp.net is “how to retain a value across pages), although there are scattered articles which give the solutions, this is an attempt to combine them and present the various solutions available.


Session Variables


This is definitely the easiest option available in .Net and most .Net freshers tend to use them, however Session variables should only be used in seldom circumstances which I will not discuss in this article.



Session data can be used to store user specific data and its usage is something like this:



Sessions are basically key-value pairs, while the key will remain the same for all users, the value will depend on the input for the user under which context the application is running.

Caution: A session variable is created for each user which means if your site is using 20 session variables and 20000 people are using your application it means 20*20000 = 400000 variables will be created in the server memory.


Supports: any object including strings, custom objects, integers etc and only serializable objects in case outProc session type is being used (default is InProc).





Cookies


Cookies is the method of adding small files into the client’s machine (temp internet files), which will contain the required information.







The advantage of using cookies is obviously that the data is completely stored in the client side and hence server is not loaded with thousands of variables data in its memory.



Supports: string data only.


Query String
An elegant way of passing information from one page across the other is to use the query string. A query string is the part of the URL which starts from the character “?” e.g.



http://blogger.com/home.aspx?Mykey=MyValue&ScondKey=SecondValue ; here “?MyKey=MyValue&ScondKey=SecondValue” is the query string.


Query strings can be accessed in the code behind like:





The value of the string variable “strValue” in the latter case would then become “MyValue”. Parameters can be differentiated by adding “&” in the query string.



Supports: string data only, in case of special characters in the query string, it can be URL encoded by using the “HTTPUtility.URLEncode” method.


Context


This method can only be used in Server.Transfers and the value persists for one transfer.


This can be done using the following code:





On the second page you can access the value simply by writing:





Supports: any object.



Static Variables


Static variables or “Shared” variables as they are known in VB have a single instance. This means that the value of the variable will remain the same for all users using the application and any changes made to the same will be reflected to all.


Shared variables will only lose their data in case the application pool or the app domain is restarted.




Supports: any object.

Web.Config


In case a variable is a constant and is used throughout the application Web.Config is a good place to store it. The appSettings section of the configuration file can be used for storing such values.




This can be accessed anywhere in the code using the following sample:




This method can be used for storing connection strings, file folder paths, application modes (if any) etc.


Supports: String data.


Thats it, please let me know if I have missed something, comments are of course welcome.

1 comment:

Followers