Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 ViewState null reference exception

Author  Topic 

jhermiz

3564 Posts

Posted - 2007-12-18 : 09:21:32
Once in a while I will get an exception like below:



I'm not sure how to handle this part

If ViewState("ReferrerUrl").ToString Is Nothing Then
Response.Redirect("default.aspx?New=" & bNew & "&Sent=" & bSentMail)
Else
If InStr(ViewState("ReferrerUrl").ToString().ToLower, "default.aspx") > 0 Then
Response.Redirect("default.aspx?New=" & bNew & "&Sent=" & bSentMail)
Else
Response.Redirect(ViewState("ReferrerUrl").ToString() & "?New=" & bNew & "&Sent=" & bSentMail)
End If
End If


As I thought:

If ViewState("ReferrerUrl").ToString Is Nothing Then

Checks to see if it is null?

Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-12-18 : 09:28:13
Is Empty?



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2007-12-18 : 09:29:58
just use:

if ViewState("ReferrerURL") Is Nothing Then ....

You cannot apply the ToString() method on a variable that is not set to an object, so if the ViewState("..") return nothing, the call to ToString() will always generate an error.

- Jeff
http://weblogs.sqlteam.com/JeffS
Go to Top of Page

jhermiz

3564 Posts

Posted - 2007-12-18 : 09:36:38
quote:
Originally posted by jsmith8858

just use:

if ViewState("ReferrerURL") Is Nothing Then ....

You cannot apply the ToString() method on a variable that is not set to an object, so if the ViewState("..") return nothing, the call to ToString() will always generate an error.

- Jeff
http://weblogs.sqlteam.com/JeffS




Makes sense thank you


Weblog -- [url]http://weblogs.sqlteam.com/jhermiz[/url]
Go to Top of Page
   

- Advertisement -