Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the UrlReferrer 1

Status
Not open for further replies.

ericnet

Programmer
Mar 29, 2006
106
When I run this code:
Code:
  Response.Write("<p>UrlReferrer:</p>")
  If Request.UrlReferrer Is DBNull.Value Then
   Response.Write("UrlReferrer = Bookmark" & "<br>")
  Else
   Response.Write("UrlReferrer = " & Request.UrlReferrer.ToString() & "<br>")
  End If

I get this error:
System.NullReferenceException: Object reference not set to an instance of an object.
Line 38: Response.Write("UrlReferrer = " & Request.UrlReferrer.ToString() & "<br>")


How can I solve it?

Thanks
 
That's because the Request.UrlReferrer won't be equal to DBNull.Value. Try changing it to:
Code:
If Request.UrlReferrer Is Nothing Then
As a side note, the user may not have necessarily come from a "bookmark" just because the referrer is null. Also, you shouldn't be using Response.Write (unless you are just testing of course).


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Yes, in this way it works! :
Code:
If Request.UrlReferrer Is Nothing Then
Thank you

As a side note, the user may not have necessarily come from a "bookmark" just because the referrer is null.
Ok. Then the value can be also null, so perhaps it is better that I check for both values (nothing and null) before I want to store the value into the DB..
Code:
If Request.UrlReferrer Is Nothing Or Request.UrlReferrer Is DBNull.Value Then

  Dim ref As String = DBNull.Value
  'Then insert that null value into the DB

Else
  Dim ref As String = Request.UrlReferrer.ToString()
  'Then insert that value into the DB 
End If
What do you think? This will prevent of any other error related to 'Request.UrlReferrer' value?

Also, you shouldn't be using Response.Write (unless you are just testing of course).
Yes, I'm testing





 
Ok. Then the value can be also null, so perhaps it is better that I check for both values (nothing and null) before I want to store the value into the DB..
Sorry, I didn't mean "null". What I meant was that just because a referrer doesn't exist (i.e. it is Nothing) that doesn't mean that they have come from a bookmark. For example, they may have searched for your site and typed the address into the address bar.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Ok. But I really need to know if there is the possibility of any other error due to 'Request.UrlReferrer' value, like I mentioned in my original post (NullReferenceException or others) that you solved it. Because when I publish my website I want to avoid the pages are not displayed due to these errors.

Indeed I have the same doubt about the possible errors that can be thrown due to the type of values of these ‘Requests’ can contain:

UserHostAddress:
Request.UserHostAddress

UserAgent:
Request.UserAgent

Requested URL:
Request.Url.ToString


-Browser capabilities-
Dim bc As HttpBrowserCapabilities = Request.Browser
Crawler:
bc.Crawler
Name:
bc.Browser
Version:
bc.Version
JavaScript enabled:
bc.JavaScript
Cookies enabled:
bc.Cookies
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top