Object not working on pageback

T

tshad

I have an object that is my position table.

I set up a global variable like so:

Dim newPosition as New Position

My Page_Load and Page PreRender routines are:
*************************************************************
Sub Page_Load(sender as Object, e as EventArgs)
dim IsCheckResumes as Boolean = true
if not IsPostBack
newPosition.JobTitle = "a new JobTitle"
JobTitle.Text = newPosition.JobTitle
trace.warn("newPosition = " & newPosition.JobTitle) <--
works
end if

newPosition = viewState("newPosition")
trace.warn("newPosition = " & newPosition.JobTitle) <--
doesn't work
end sub

Sub Page_PreRender(ByVal Sender As Object, ByVal E As EventArgs)
viewstate("newPosition") = newPosition
End Sub

**************************************************************

On the page back I get the error:

**************************************************************
Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 36: end if
Line 37: newPosition = viewState("newPosition")
Line 38: trace.warn("newPosition = " & newPosition.JobTitle) <-- error
Line 39: end sub
Line 40:
****************************************************************************I thought you could put objects in the viewState?What about a session variable?Thanks,Tom
 
T

tshad

I found that this is only a problem with viewState.

If I put it in a session variable, like so:

session("newPosition") = newPosition
newPosition = session("newPosition")

it seems to work fine (as long as I mark the object as Serializable).

Why won't it work in viewState?

Thanks,

Tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top