justinkent
asked on
Storing an Object Reference in Session (VB.NET)
Hi -
I want to store an object reference in memory so I can access user info from page to page. It's a 2-part question, the first being how do I store the object reference in session, and secondly, how do I access it?
The first part I cannot get to work and the following is a simplified version of my code:
On my aspx page, either of the following does not work:
or
Both ways throw an error of: "Expression does not produce a value".
I understand why it's throwing an error, but can't understand how to just reference the object with it's filled variables in the session, so I can then access it on other pages.
Thanks.
I want to store an object reference in memory so I can access user info from page to page. It's a 2-part question, the first being how do I store the object reference in session, and secondly, how do I access it?
The first part I cannot get to work and the following is a simplified version of my code:
Namespace User
Public Class User
Public CID As String = ""
Public UserID As String = ""
Public UserName As String = ""
Public UserFirstName As String = ""
Public UserSurname As String = ""
Public Sub GetUser(ByVal UID As String)
(code here would go to the DB and get the user based in the supplied UID)
CID = "123456"
UserID = "567890"
UserName = "fbloggs"
UserFirstName = "Fred"
UserSurname = "Bloggs"
End Sub
End Class
End Namespace
On my aspx page, either of the following does not work:
Dim UserData As New User.User
Session("Test") = UserData.GetUser(UN)
or
Dim UserData As New User.User
dim UserTest as Object
UserTest = UserData.GetUser(UN)
Session("Test") = UserTest
Both ways throw an error of: "Expression does not produce a value".
I understand why it's throwing an error, but can't understand how to just reference the object with it's filled variables in the session, so I can then access it on other pages.
Thanks.
Well there is nothing wrong with the way you are trying to assign to the session, so there must be a problem with either the User class itself, or the GetUser() method.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks very much Carl, that does now allow me to store the object in the session.
And how would I then access the variables in that object in subsequent pages?
And how would I then access the variables in that object in subsequent pages?
ASKER
Actually, I think I figured it:
Something like this?
It works... Would this be correct and proper way?
Something like this?
Dim testData As User.User = Session("Test")
Response.Write("CID = " & testData.CID)
It works... Would this be correct and proper way?
You can't read properties from the object directly from the session. You need to pull your object out first and cast it to the correct type, then you can work with it:
Dim user As UserData = CType(Session("Test"), UserData)
'// quick sanity check
If Not user Is Nothing Then
'// we have a valid object so we can now read from it
Response.Write(user.UserFirstName)
End If
ASKER
Fantastic, thank you.
:o)
:o)