473,385 Members | 1,940 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1515
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Paul Yanzick | last post by:
Hello, I am trying to develop a book tracking application for my capstone in school, and am running into a problem. The application is an ASP.Net application written in C#. The first page you...
3
by: Nhi Lam | last post by:
Hi, I understand that there are 3 modes in which I can configure the SessionStateModule. What I need is an out of process Session State store with fail over support. The "SQL Server Mode" seems...
5
by: Oleg Ogurok | last post by:
Hi all, Is there a way to read other people's session variables? I understand it makes sense that session state is on per-user basis, but still... Is there a way to get a collection of all...
9
by: McGeeky | last post by:
Is there a way to get a user control to remember its state across pages? I have a standard page layout I use with a header and footer as user controls. Each page uses the same layout by means of...
5
by: jb | last post by:
*Please* help --- I'm tearing my hair out. I want to use sessionstate in a webservice, accessed from a client, written in script (JScript, InfoPath). I have written my webservice (C# .NET). I...
5
by: Steven Blair | last post by:
I have the following code: Session = new CurrentUser("TEST"); When I postback to the server, the Session is null. My guess is a only the refence to my actual class is stored, rather than the...
8
by: Azrael | last post by:
Hi, I have an SSLStream and i want to resume the SSL-Session for another connection to this server. How can i do this? I haven´t found any clues for it in SSLStream, perhaps Negotiatestream...
6
by: Bhagya | last post by:
Hello, On the LogOut Page i have done Session.Abandon(); And on every Page, In the Page_Load Event i check if the session exists and only then display data. Now the problem is after i logout from...
11
by: Glenn | last post by:
Hi I've been experimenting with managing state using the Session object. I've created a simple WS with a couple of methods, one which sets a string value, another that retrieves it. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.