Saturday, March 24, 2012

Session Variables

Is it a good idea to use session variables in ASP.NET. Is there something better which can be used. Basically I am developing an application in which the user logs in using forms authentication and starts a session with the web server. I also need to pass some variables across the pages - so basically a typical web app.

I have heard that there s no need to pass around variables in ASP.NET . Wat does that means? Wats the alternative provided by ASP.NET - is it because of the code behind functionality?

A point to a good article will also be appreciated.

Thanks.I was struggling with the same promblem as you are. I also used session variables to store additional user information after logon. However the session kept timing out so instead of using a session I used a cookie (just like the forms authentication does) and it works fine. I used the following syntax:


'-- Write information to cookie
objCookie = New HttpCookie("UserInformation")
objCookie.Values("UserName") = "maurits"
objCookie.Values("UserID") = 1
objCookie.Values("IsAdmin") = True
objCookie.Values("Statistics") = True
Response.Cookies.Add(objCookie)

'-- Read information from cookie
strUserName = Request.Cookies("UserInformation").Values("UserName")
intUserID = Request.Cookies("UserInformation").Values("UserID")
blnAdmin = Request.Cookies("UserInformation").Values("IsAdmin")
blnStats = Request.Cookies("UserInformation").Values("Statistics")

Maybe this helps you or maybe it won't but at least I tried ;-)

Regards Maurits
If you want to rely on your client to hold the info, then sure, cookies are great. But what if some of your users configure their browsers to disable cookies? Not good at all. The general consensus is to store private and sensitive information in session variables and the rest of your data in the query string. (www.blah.com?query=string&var=isthis). While you are developing, be sure to enable page tracing so you can see the size of the data you are storing in sesssion, because the size of session vars has a direct impact on how fast your page will load.

Good Luck!
Thanks guys, I still have one question un-answered from my original question:

"I have heard that there s no need to pass around variables in ASP.NET . Wat does that means? Wats the alternative provided by ASP.NET - is it because of the code behind functionality? "

Well, after looking at the date of this post, I'm sure your question has been answered by now but just in case and for future reference of other new-comers to ASP.net, here's the answer...

Session variables are initialized and maintained in your Global.asax file. These variables are declared there, initialized when a user session starts, and you can set, get, and update their values by just calling them. In your Global.asax file, you would simply declare a few session variables on Session_Start like this:

Sub Session_Start(ByVal senderAs Object,ByVal eAs EventArgs) Session("CurrUserID") ="" Session("CurrUserPass") ="" Session("CurrUserEmail") ="" Session("CurrUserLname") ="" Session("CurrUserFname") ="" Session("CurrUserGroup") ="" Session("ConnectionString") =""End Sub

So, as you can see, using session variables can be very helpful when you need user-specific data to be stored and accessed in a web app since multiple users will be accessing the same server and same pages. Anywhere in your web app you can access and/or set values of these session variables by simply calling them directly. Like the following:

' To Set The Value Of A Session Variable:Session("CurrUserID") = txtUserID.text
' To Use The Value Of A Session Variable:txtUserID.text = Session("CurrUserID")

Hope This Helps (Even Though I'm Pretty Sure It's Too Late),
gOMER

0 comments:

Post a Comment