Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Suppose I create a custom class object, and I want this object to persist throught the pages of an on-line survey. How would I go about ensuring this persistence? Can I instantiate the object in the global.aspx Sub Session_Start(Sender As Object, E As EventArgs)? Or, can I assign this custom object as a property of the Session Object?

 

 

i.e

Sub Session_Start(Sender As Object, E As EventArgs)

Dim myCustObj as MyCustomClassObject

   Session("UserObject") = myCustObj

End Sub

  • *Experts*
Posted

Yes, assign it to the Session variable like you're currently doing.

Then, to access it from a page, just cast it to the correct type.

 

For example:

 

Dim myCustObj As MyCustomClassObject

myCustObj = DirectCast(Session("UserObject"), MyCustomClassObject)
' Here you manipulate myCustObj

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • Moderators
Posted

To add to Bucky's post...

 

In case myCustObj does not contain all expected values during Session_start you can assign to it from any page using the same method.

Session("UserObject") = myCustObj

Visit...Bassic Software
Posted

Very nice. Thanks guys.

 

Robby, do you mean that if I change the values in the object on a page, I have to re-assign the object to the Session object, so the new data in the object is available on the next page?

  • *Experts*
Posted
Yes, that's correct.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

  • 1 month later...
Posted

I'm looking for a similar solution, but have hit a snag.

 

I've created a custom Product class and compiled it. I can access it on the individual page using the following code:

 

<%
 Dim objProduct As Product
 objProduct = New Product()

 objProduct.ID = 123456
 objProduct.PartNumber = "Part Number 12345"
 objProduct.Description = "This is the description of the product"
 objProduct.Category = 1
 objProduct.QuantityPerUnit = 1
 objProduct.UnitSize = "each"
 objProduct.UnitSellPrice = 24.99

%>

 

However, when I try to declare a Product variable in my global.asax file as in your example:

 

   Sub Session_Start(Sender As Object, E As EventArgs)
       Dim objProduct As Product
       Session("objProduct") = objProduct
   End Sub

 

I get a error message saying "Compiler Error Message: BC30002: Type 'Product' is not defined." I know its not a problem with my Product class, since I can declare and manipulate it on other pages, but when I try to declare it in the session variable, it fails.

 

Any ideas?

  • *Experts*
Posted

The problem does not lie in the setting of the variable to a

Session variable; it lies in the declaration of objProduct. Make

sure that the page has the correct Imports statement to

import the namespace that Product is declared in.

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...