470,594 Members | 1,137 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,594 developers. It's quick & easy.

Session State Class Reuse?

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?
Nov 18 '05 #1
8 1415
Leon wrote:
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
Nov 18 '05 #2
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" <vn*****@msn.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
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?

Nov 18 '05 #3
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 news:et**************@TK2MSFTNGP12.phx.gbl...
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" <vn*****@msn.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
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?


Nov 18 '05 #4
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" <vn*****@msn.com> wrote in message
news:uC****************@TK2MSFTNGP10.phx.gbl...
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 news:et**************@TK2MSFTNGP12.phx.gbl...
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" <vn*****@msn.com> wrote in message
news:O$**************@tk2msftngp13.phx.gbl...
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?



Nov 18 '05 #5
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" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:eG***************@TK2MSFTNGP10.phx.gbl...
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" <vn*****@msn.com> wrote in message
news:uC****************@TK2MSFTNGP10.phx.gbl...
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 news:et**************@TK2MSFTNGP12.phx.gbl...
> 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" <vn*****@msn.com> wrote in message
> news:O$**************@tk2msftngp13.phx.gbl...
>> 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?
>>
>>
>
>



Nov 18 '05 #6
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" <vn*****@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:eG***************@TK2MSFTNGP10.phx.gbl...
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" <vn*****@msn.com> wrote in message
news:uC****************@TK2MSFTNGP10.phx.gbl...
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 news:et**************@TK2MSFTNGP12.phx.gbl...
> 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" <vn*****@msn.com> wrote in message
> news:O$**************@tk2msftngp13.phx.gbl...
>> 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?
>>
>>
>
>



Nov 18 '05 #7
Thanks Karl for all the help!

"Karl Seguin" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:uh**************@TK2MSFTNGP10.phx.gbl...
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" <vn*****@msn.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
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" <karl REMOVE @ REMOVE openmymind REMOVEMETOO . ANDME net>
wrote in message news:eG***************@TK2MSFTNGP10.phx.gbl...
> 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" <vn*****@msn.com> wrote in message
> news:uC****************@TK2MSFTNGP10.phx.gbl...
>> 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 news:et**************@TK2MSFTNGP12.phx.gbl...
>> > 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" <vn*****@msn.com> wrote in message
>> > news:O$**************@tk2msftngp13.phx.gbl...
>> >> 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?
>> >>
>> >>
>> >
>> >
>>
>>
>
>



Nov 18 '05 #8
Leon wrote:
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,

--
Joerg Jooss
www.joergjooss.de
ne**@joergjooss.de
Nov 18 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by Paul Yanzick | last post: by
3 posts views Thread by Nhi Lam | last post: by
5 posts views Thread by Oleg Ogurok | last post: by
5 posts views Thread by Steven Blair | last post: by
6 posts views Thread by Bhagya | last post: by
11 posts views Thread by Glenn | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.