• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 225
  • Last Modified:

Class and Session Variable

Hello Experts,
I have a class that I call data that contains a bunch of properties.  As the user moves through the pages, certian properties are updated and then written to the database at the end.  I am trying to save this class to a session variable like below
but I keep getting the "Object reference not set to an instance of an object." error. The first time a save it to the Session variable it works because I use:
 Private cdata As Data = New Dat
 cdata = Session("DataClass")

Then on the next page I try to access the Session, write the properties then save it back to the session.

        Dim gdata As data = Session("DataClass")
        gdata.Subtotal = Me.txtSub.Text
        gdata.Tax = Me.txtTax.Text
        gdata.SH = Me.txtSH.Text
        gdata.TotalPrice = Me.txtTotal.Text
        Session("DataClass") = gdata

This is when the error occurs.  I was wondering what I am doing wrong.
Thanks
Chad
0
ChadMarsh
Asked:
ChadMarsh
  • 5
  • 4
  • 3
  • +1
3 Solutions
 
praneethaCommented:
well

i guess u can do the same thing in every page then

Dum gdata as data=new data
gdate=Session("DataClass")


0
 
dharmesh_amityCommented:
I think you are trying to use the Session("DataClass")  before you initialize it.

Make sure first you do this.

Dim cdata As Data = New Dat
 Session("DataClass") =cdata

After that you can access it by

Dim cdata as Data

cdata =  Session("DataClass")
0
 
praneethaCommented:
hmm yeah he is right...u r not storing into session variable the first time.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
dharmesh_amityCommented:
Dim cdata As Data = New Dat
 Session("DataClass") =cdata

This can be done in your Session_Start event so that you are sure that you have Session("DataClass") initialized when a new session is started
0
 
ChadMarshAuthor Commented:
On my first page I have  
  Private cdata as data = new data

Then right before I go to the next page I set the session
  Session("DataClass") = cdata

Then the next time I want to access it I declare


0
 
ChadMarshAuthor Commented:
accidently hit enter..
is it getting lost inbetween the 2 pages?
0
 
praneethaCommented:
may be u should type cast it..using CType...
0
 
praneethaCommented:
no it should not get lost inbetween 2 pages of same webapplication
0
 
dharmesh_amityCommented:
Check if you have EnableSessionState=false on any of those pages directives.
0
 
TorrwinCommented:
When I store a dataset in the Session variables I have to do it like this:
Dim dsItemGrid As DataSet = CType(Session("myData"), DataSet)

So, I would suggest trying this:
Dim gData As Data = CType(Session("DataClass"), Data)
0
 
ChadMarshAuthor Commented:
No Luck...I've tried all the suggestions.  I am using a dataset session variable on the same page and it works fine.
Here is more of my code.  It's a 4 page point of sale application.
The first page is the products page.  In the declarations section of the this page I declare
MainData as data = new data.
Then I write a few properties and when the user clicks the next button It executes
Session("DataClass")=MainData
The next page is an overview of the cart (which is a session variable of a datatable)
When the user clicks next on this page the following code executes.
Private Sub btnnext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnnext.Click
        subtotal()
        Dim gData As data = CType(Session("DataClass"), data)
        gData.Subtotal = Me.txtSub.Text
        gData.Tax = Me.txtTax.Text
        gData.SH = Me.txtSH.Text
        gData.TotalPrice = Me.txtTotal.Text
        Session("DataClass") = gData
        Response.Redirect("Billing.aspx")
    End Sub

This contains the last example Torrwin gave me.  I have tried declaring gData as data = new data then
gData = Session("DataClass") and I have also tried Dim gData as Data = Session("DataClass") right in the btnnext sub
and I get the null reference error on all.  All of my table Session variables are working and I did not find any EnableSessionState=false.

I really appreciate all of your help.  
Chad
0
 
ChadMarshAuthor Commented:
Here is the declaration section of my class just to make sure I'm not missing anything there
Imports System.Data.SqlClient
Imports System.Data
Public Class data
0
 
ChadMarshAuthor Commented:
Thanks to all of you.  I found another error in my code that was causing the problem.  Since you were all correct and so kind in offering your suggestions, I split the points.
Thanks again
Chad Marsh
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now