Hi, I have the following code in my ASPX:
private string pagina =
System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"].ToUpper());
The problem is it: When i call this page directly i get an error:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
I really need that, but i do not know how to accomplish this. Does someone
can help me ?
Kevin Spencer - 19 Jul 2005 22:14 GMT
If there is no HTTP_REFERER (no page from which this page was navigated to),
the ServerVariable will be null. You need to check for this, as "ToUpper()"
will throw an exception if the string is null.

Signature
HTH,
Kevin Spencer
Microsoft MVP
.Net Developer
The sun never sets on
the Kingdom of Heaven
> Hi, I have the following code in my ASPX:
> private string pagina =
[quoted text clipped - 12 lines]
> I really need that, but i do not know how to accomplish this. Does someone
> can help me ?
Mark Fitzpatrick - 19 Jul 2005 22:17 GMT
All it's saying is that there is no value for the HTTP_Referer item, that
means that the array item will be a null value. It's throwing an error
because you cannot convert a null value into an uppercase string, or any
string for that matter.
You have to first check and see if it's null before you attempt to access it
if(Request.ServerVariables["HTTP_REFERER"] != null)
{
// then you can do something
}
If you have to count on the referer information don't, you don't always
receive information so a lot of the times you'll simply have a null value
here.
Hope this helps,
Mark Fitzpatrick
Microsoft MVP - FrontPage
> Hi, I have the following code in my ASPX:
> private string pagina =
[quoted text clipped - 12 lines]
> I really need that, but i do not know how to accomplish this. Does someone
> can help me ?
John Timney (ASP.NET MVP) - 19 Jul 2005 22:19 GMT
your referer is probably an empty string if your not referred to the page
from somewhere- your trying to getfilename on an empty string hence the
exception, use a try catch or check the value of referrer first

Signature
Regards
John Timney
ASP.NET MVP
Microsoft Regional Director
> Hi, I have the following code in my ASPX:
> private string pagina =
[quoted text clipped - 12 lines]
> I really need that, but i do not know how to accomplish this. Does someone
> can help me ?
Paperback Writer - 19 Jul 2005 22:28 GMT
I can not check with clause IF because it's out of a method...it's a private
string into the class!
> your referer is probably an empty string if your not referred to the page
> from somewhere- your trying to getfilename on an empty string hence the
[quoted text clipped - 16 lines]
>> I really need that, but i do not know how to accomplish this. Does
>> someone can help me ?
Kan Coon - 20 Jul 2005 07:59 GMT
The fact that you can do this:
System.Web.HttpContext.Current.Request.ServerVariables["HTTP_REFERER"].ToUpp
er());
says you DO have access to the null string in question.
Richard
> I can not check with clause IF because it's out of a method...it's a private
> string into the class!
[quoted text clipped - 5 lines]
> >> Hi, I have the following code in my ASPX:
> >> private string pagina =
System.IO.Path.GetFileName(System.Web.HttpContext.Current.Request.ServerVari
ables["HTTP_REFERER"].ToUpper());
> >> The problem is it: When i call this page directly i get an error:
> >>
[quoted text clipped - 8 lines]
> >> I really need that, but i do not know how to accomplish this. Does
> >> someone can help me ?