Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi ,

I am trying to get the urlreferrer of the external site.
But i am getting error 'Object reference not set to an instance of object'

if (!IsPostBack)
{
string MyReferrer = Request.UrlReferrer.ToString();
lblReferrer.Text = MyReferrer.ToString();
}

Please help me

Thanks
Posted

1 solution

Apparently, UrlReferrer can be null, which would always throw the exact exception you observe. The browser gives you the referral data when the user clicks the anchor with the URL of your page, but what if the user simply type its URL in the address line? The referrer would be null, so you need to take it into account.

Besides, what are you doing?! Of course, HttpRequest.UrlReferrer is System.Uri, so you can use ToString, but your MyReferrer is already the string, why are you trying to calculate ToString of a string? Please see:
http://msdn.microsoft.com/en-us/library/system.web.httprequest.urlreferrer.aspx[^],
http://msdn.microsoft.com/en-us/library/system.uri.aspx[^].

—SA
 
Share this answer
 
Comments
YesPriya 1-Oct-12 13:09pm    
string MyUrl = HttpContext.Current.Request.UrlReferrer.ToString();
lblReferrer.Text = MyUrl;

Still i am getting object reference not set to instance of object from the above code.Can you suggest me how to write the code for getting the URLReferrer
I want to bind that referrer url to label and show it on the screen
Sergey Alexandrovich Kryukov 1-Oct-12 13:22pm    
Of course, I explained it in the 1st paragraph of the answer -- there is no referral, can you understand it? The exception is in ToString: you de-referencing the referral, which is null. Check it for null (do UrlReferrer.ToString in next line only, when it is not null). If it is null, assign lblReferrer.Text = string.Empty, OK?
--SA
Sergey Alexandrovich Kryukov 1-Oct-12 13:55pm    
Priya, please pay attention.

I answered in full and invited you to ask follow-up questions if you did not get it. I removed two of your re-posts and explained why re-posts could be no good for you. Despite of my warning, you re-posted again. This is not the way to get help -- not at all.

You apparently have very low experience, but people here demonstrated a lot of patience and the good will to help you. Don't press it -- it cannot help you. You need to pay some attention and use some patience, too. What, for example is not clear from this answer? If you need help, you need to feedback accordingly. Ask the question, and when you finally get it, it would also be nice to report that it works for you.

Please, my invitation to ask your follow-up questions is still in place -- you can ask what else is not working and wait until I answer, but it could be somebody else who would help you further.

--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900