Link to home
Start Free TrialLog in
Avatar of ChadMarsh
ChadMarshFlag for United States of America

asked on

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
SOLUTION
Avatar of praneetha
praneetha

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of praneetha
praneetha

hmm yeah he is right...u r not storing into session variable the first time.
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
Avatar of ChadMarsh

ASKER

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


accidently hit enter..
is it getting lost inbetween the 2 pages?
may be u should type cast it..using CType...
no it should not get lost inbetween 2 pages of same webapplication
Check if you have EnableSessionState=false on any of those pages directives.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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
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