ASP.net Request.UrlReferrer.Authority returning a object not set to reference of an object.

Here is the code:
                Dim sPreviousURL = Request.UrlReferrer.Authority
                If Not sPreviousURL = "" Then
                    Dim oSQLInsertDelete As SqlConnection = New SqlConnection("Connection")
                    Dim commandInsert As New SqlCommand("INSERT INTO Clean (InvalidCampaigns, Referrer) VALUES ('" & Request.QueryString("user") & "', '" & sPreviousURL & "')", oSQLInsertDelete)
                    oSQLInsertDelete.Open()
                    commandInsert.ExecuteReader()
                    oSQLInsertDelete.Close()
                End If
The statement "Request.UrlReferrer.Authority" is returning the error message. I've tried to research it all over the internet and all the solutions have gone unsolved. I could really use some help. Thanks, Chris.
jumpstart0321Asked:
Who is Participating?
 
TechTiger007Commented:
When Request.UrlReferrer is null Request.UrlReferrer.Authority will throw an exception

You can check if Request.UrlReferrer is Nothing before accessing Authority

if Not Request.UrlReferrer is Nothing then
sPreviousURL = Request.UrlReferrer.Authority
0
 
TechTiger007Commented:
There is no guarantee that you will have a valid value in UrlReferrer. If you are using HTTPS protocol you will find it null.
0
 
jumpstart0321Author Commented:
Shouldn't this be handled by my statement:
If Not sPreviousURL = "" Then
<Process data>
If that doesn't work, can you suggest what I should use?Thanks.
0
 
jumpstart0321Author Commented:
Got it. for public reference purposes, here is the code I used:
            Try
                If Not String.IsNullOrEmpty(Request.UrlReferrer.Authority) Then
                    'Response.Write(Request.UrlReferrer.Authority)
                    If Not Request.UrlReferrer.Authority.Contains("webspider") Then
                        Dim oSQLInsertDelete As SqlConnection = New SqlConnection("Connection")
                        Dim commandInsert As New SqlCommand("INSERT INTO Referrer (Referrer) VALUES ('" & Request.UrlReferrer.Authority & "')", oSQLInsertDelete)
                        oSQLInsertDelete.Open()
                        commandInsert.ExecuteReader()
                        oSQLInsertDelete.Close()
                    End If
                End If
            Catch ex As Exception
            End Try
0
 
jumpstart0321Author Commented:
Thanks man, much appreciated.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.