ASP.NET Error - Object Ref Not Set...

G

Guest

The following code segment throws an "Object Reference Not Set to an Instance
of an Object" error.
-------
Public Sub Page_Load(Source As Object, E As EventArgs)
Dim viewCount As Integer
If ViewState("viewCount").ToString() = "" Then
viewCount = 1
Else
viewCount = CType(ViewState("viewCount"), Integer) + 1
End If
labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
ViewState("viewCount") = viewCount
End Sub
 
S

Shiva

Does ViewState("viewCount") have a value? Check what it returns. Or you
might want to chech for postback and access it?

The following code segment throws an "Object Reference Not Set to an
Instance
of an Object" error.
-------
Public Sub Page_Load(Source As Object, E As EventArgs)
Dim viewCount As Integer
If ViewState("viewCount").ToString() = "" Then
viewCount = 1
Else
viewCount = CType(ViewState("viewCount"), Integer) + 1
End If
labelViews.Text = "Times page has been viewed: " & viewCount.ToString()
ViewState("viewCount") = viewCount
End Sub
 
G

Guest

Thanks Shiva:

ViewState("ViewCount") does not have a value. In the code snippet we set the
value of viewCount to 1 if this is the case.
The code still does not work.
 
S

Shiva

You might want to do something like this:

If (ViewState("viewcount") Is Nothing) Then
viewCount = 1
Else
viewCount = CType(ViewState("viewCount"), Integer) + 1
End If

ViewState("viewCount") = viewCount
....
....

HTH

Thanks Shiva:

ViewState("ViewCount") does not have a value. In the code snippet we set the
value of viewCount to 1 if this is the case.
The code still does not work.
 

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

Top