?
Solved

Storing an Object Reference in Session  (VB.NET)

Posted on 2011-03-14
6
Medium Priority
?
568 Views
Last Modified: 2013-11-27
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:

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

Open in new window



On my aspx page, either of the following does not work:

Dim UserData As New User.User
Session("Test") = UserData.GetUser(UN)

Open in new window


or

Dim UserData As New User.User
dim UserTest as Object
UserTest = UserData.GetUser(UN)
Session("Test") = UserTest

Open in new window


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.
0
Comment
Question by:justinkent
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35128306
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.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 35128320
Actually reading your code (properly this time). Your GetUser() method is a sub, not a function, so it doesn't return a value, hence you cannot directly assign it to the session.

You would have to do:
Dim UserData As New User.User
UserData.GetUser(UN)

Session("Test") = UserData

Open in new window

0
 

Author Comment

by:justinkent
ID: 35128648
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?
0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

Author Comment

by:justinkent
ID: 35128681
Actually, I think I figured it:

Something like this?

   Dim testData As User.User = Session("Test")
   Response.Write("CID = " & testData.CID)

Open in new window


It works...  Would this be correct and proper way?
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35128693
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

Open in new window

0
 

Author Comment

by:justinkent
ID: 35128743
Fantastic, thank you.

:o)
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question