Session State Class Reuse?

L

Leon

I know that I can access session state on an asp.net page
using Page objects, but how do I access store data in sessionstate from the
middle tiers?
 
H

Hans Kesting

Leon said:
I know that I can access session state on an asp.net page
using Page objects, but how do I access store data in sessionstate
from the middle tiers?

System.Web.HttpContext.Current.*

Works only if you are really in an HttpContext, that is: the call to your
method is caused by a Request (not a timer-tick or session_end)

Hans Kesting
 
K

Karl Seguin

Use System.Web.HttpContext.Current.Session ala:

HttpContext context = System.Web.HttpContext.Current;
if (context == null){ //not in a web environment
throw new exception("Function BLAH must be called from within a web
context");
}
string value = context.Session["blah"];

Karl
 
L

Leon

So would this work? and how would I access the stored session data from
within my page?

Imports System.web

' Constructors
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -

Public Sub LoadSession(ByVal ChoosenEmailAddress As String)
myEmailAddress = ChoosenEmailAddress
LoadFromEmail()
End Sub

' Private Code
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -

Private Sub LoadFromEmail()

Dim dataUser As New Data.Student
Dim userRow As DataRow =
dataUser.RetrieveAccount(myEmailAddress)
Dim context As HttpContext = System.Web.HttpContext.Current

myFirstName = context.Session(CStr(userRow("FirstName")))
myLastName = context.Session(CStr(userRow("LastName")))
myPassword = context.Session(CStr(userRow("Password")))
myEmailAddress = context.Session(CStr(userRow("EmailAddress")))

End Sub

Karl Seguin said:
Use System.Web.HttpContext.Current.Session ala:

HttpContext context = System.Web.HttpContext.Current;
if (context == null){ //not in a web environment
throw new exception("Function BLAH must be called from within a web
context");
}
string value = context.Session["blah"];

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Leon said:
I know that I can access session state on an asp.net page
using Page objects, but how do I access store data in sessionstate from the
middle tiers?
 
K

Karl Seguin

Leon:
Your code confuses me slightly. It seems to me you are getting the user
data from the dataUser.RetrieveAccount function which returns a
DataRow...not sure why you need sessions in this case.

context.Session(Cstr(userRow("FirstName")))

I'm not sure what you are trying to do with the above line, couldn't you
just do:

myFirstName = cstr(userRow("FirstName")) ??

why do you need sessions at all?


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Leon said:
So would this work? and how would I access the stored session data from
within my page?

Imports System.web

' Constructors
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -

Public Sub LoadSession(ByVal ChoosenEmailAddress As String)
myEmailAddress = ChoosenEmailAddress
LoadFromEmail()
End Sub

' Private Code
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -

Private Sub LoadFromEmail()

Dim dataUser As New Data.Student
Dim userRow As DataRow =
dataUser.RetrieveAccount(myEmailAddress)
Dim context As HttpContext = System.Web.HttpContext.Current

myFirstName = context.Session(CStr(userRow("FirstName")))
myLastName = context.Session(CStr(userRow("LastName")))
myPassword = context.Session(CStr(userRow("Password")))
myEmailAddress = context.Session(CStr(userRow("EmailAddress")))

End Sub

Karl Seguin said:
Use System.Web.HttpContext.Current.Session ala:

HttpContext context = System.Web.HttpContext.Current;
if (context == null){ //not in a web environment
throw new exception("Function BLAH must be called from within a web
context");
}
string value = context.Session["blah"];

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Leon said:
I know that I can access session state on an asp.net page
using Page objects, but how do I access store data in sessionstate from the
middle tiers?
 
L

Leon

I dispose of the dataset, I only use the function to retrieve the data, but
by putting the data in session state I can use myFirstName, myLastName, etc.
throughout my web application. Right?

Karl Seguin said:
Leon:
Your code confuses me slightly. It seems to me you are getting the user
data from the dataUser.RetrieveAccount function which returns a
DataRow...not sure why you need sessions in this case.

context.Session(Cstr(userRow("FirstName")))

I'm not sure what you are trying to do with the above line, couldn't you
just do:

myFirstName = cstr(userRow("FirstName")) ??

why do you need sessions at all?


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Leon said:
So would this work? and how would I access the stored session data from
within my page?

Imports System.web

' Constructors
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- -

Public Sub LoadSession(ByVal ChoosenEmailAddress As String)
myEmailAddress = ChoosenEmailAddress
LoadFromEmail()
End Sub

' Private Code
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- -

Private Sub LoadFromEmail()

Dim dataUser As New Data.Student
Dim userRow As DataRow =
dataUser.RetrieveAccount(myEmailAddress)
Dim context As HttpContext = System.Web.HttpContext.Current

myFirstName = context.Session(CStr(userRow("FirstName")))
myLastName = context.Session(CStr(userRow("LastName")))
myPassword = context.Session(CStr(userRow("Password")))
myEmailAddress = context.Session(CStr(userRow("EmailAddress")))

End Sub

Karl Seguin said:
Use System.Web.HttpContext.Current.Session ala:

HttpContext context = System.Web.HttpContext.Current;
if (context == null){ //not in a web environment
throw new exception("Function BLAH must be called from within a web
context");
}
string value = context.Session["blah"];

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


I know that I can access session state on an asp.net page
using Page objects, but how do I access store data in sessionstate
from
the
middle tiers?
 
K

Karl Seguin

I don't see where you are putting it in the session...you want to do
something like this:

First time the user is loaded
Get the data from the database
store it in the sesssion:
context.Session("firstName", cstr(userRow("FirstName")))
context.Session("LastName", cstr(userRow("LastName")))

on subsequent requests, instead of getting it from the database, you can
simply get it from:
context.Session("firstName")

Also, instead of storing a bunch of indivdual fields, you might want to
consider creating a "user" class, and simply storing that

Dim u as new User()
u.FirstName = cstr(userRow("FirstName"))
u.LastName= cstr(userRow("LastName"))
context.Session("User", u)


and retrieving it via:

Dim u as User = ctype(session("User", User))

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Leon said:
I dispose of the dataset, I only use the function to retrieve the data, but
by putting the data in session state I can use myFirstName, myLastName, etc.
throughout my web application. Right?

Karl Seguin said:
Leon:
Your code confuses me slightly. It seems to me you are getting the user
data from the dataUser.RetrieveAccount function which returns a
DataRow...not sure why you need sessions in this case.

context.Session(Cstr(userRow("FirstName")))

I'm not sure what you are trying to do with the above line, couldn't you
just do:

myFirstName = cstr(userRow("FirstName")) ??

why do you need sessions at all?


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Leon said:
So would this work? and how would I access the stored session data from
within my page?

Imports System.web

' Constructors
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- -

Public Sub LoadSession(ByVal ChoosenEmailAddress As String)
myEmailAddress = ChoosenEmailAddress
LoadFromEmail()
End Sub

' Private Code
'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- -

Private Sub LoadFromEmail()

Dim dataUser As New Data.Student
Dim userRow As DataRow =
dataUser.RetrieveAccount(myEmailAddress)
Dim context As HttpContext = System.Web.HttpContext.Current

myFirstName = context.Session(CStr(userRow("FirstName")))
myLastName = context.Session(CStr(userRow("LastName")))
myPassword = context.Session(CStr(userRow("Password")))
myEmailAddress = context.Session(CStr(userRow("EmailAddress")))

End Sub

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message Use System.Web.HttpContext.Current.Session ala:

HttpContext context = System.Web.HttpContext.Current;
if (context == null){ //not in a web environment
throw new exception("Function BLAH must be called from within a web
context");
}
string value = context.Session["blah"];

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


I know that I can access session state on an asp.net page
using Page objects, but how do I access store data in sessionstate
from
the
middle tiers?
 
L

Leon

Thanks Karl for all the help!

Karl Seguin said:
I don't see where you are putting it in the session...you want to do
something like this:

First time the user is loaded
Get the data from the database
store it in the sesssion:
context.Session("firstName", cstr(userRow("FirstName")))
context.Session("LastName", cstr(userRow("LastName")))

on subsequent requests, instead of getting it from the database, you can
simply get it from:
context.Session("firstName")

Also, instead of storing a bunch of indivdual fields, you might want to
consider creating a "user" class, and simply storing that

Dim u as new User()
u.FirstName = cstr(userRow("FirstName"))
u.LastName= cstr(userRow("LastName"))
context.Session("User", u)


and retrieving it via:

Dim u as User = ctype(session("User", User))

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


Leon said:
I dispose of the dataset, I only use the function to retrieve the data, but
by putting the data in session state I can use myFirstName, myLastName, etc.
throughout my web application. Right?

Karl Seguin said:
Leon:
Your code confuses me slightly. It seems to me you are getting the
user
data from the dataUser.RetrieveAccount function which returns a
DataRow...not sure why you need sessions in this case.

context.Session(Cstr(userRow("FirstName")))

I'm not sure what you are trying to do with the above line, couldn't
you
just do:

myFirstName = cstr(userRow("FirstName")) ??

why do you need sessions at all?


Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


So would this work? and how would I access the stored session data
from
within my page?

Imports System.web

' Constructors

'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- -

Public Sub LoadSession(ByVal ChoosenEmailAddress As String)
myEmailAddress = ChoosenEmailAddress
LoadFromEmail()
End Sub

' Private Code

'- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- -

Private Sub LoadFromEmail()

Dim dataUser As New Data.Student
Dim userRow As DataRow =
dataUser.RetrieveAccount(myEmailAddress)
Dim context As HttpContext =
System.Web.HttpContext.Current

myFirstName = context.Session(CStr(userRow("FirstName")))
myLastName = context.Session(CStr(userRow("LastName")))
myPassword = context.Session(CStr(userRow("Password")))
myEmailAddress =
context.Session(CStr(userRow("EmailAddress")))

End Sub

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME
net>
wrote in message Use System.Web.HttpContext.Current.Session ala:

HttpContext context = System.Web.HttpContext.Current;
if (context == null){ //not in a web environment
throw new exception("Function BLAH must be called from within a
web
context");
}
string value = context.Session["blah"];

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/


I know that I can access session state on an asp.net page
using Page objects, but how do I access store data in sessionstate
from
the
middle tiers?
 
J

Joerg Jooss

Leon said:
I know that I can access session state on an asp.net page
using Page objects, but how do I access store data in sessionstate
from the middle tiers?

You don't. That would couple your middle tier to your web tier, rendering it
impossible to reuse. It also obfuscates your middle tier's API, as passing
around HttpSessionState is no better than passing around object arrays or
collections with arbitrary content, and may subtly induce statefulness.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top