473,757 Members | 8,085 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing SessionState in Application_Err or

Hi everyone,

I am trying to create a custom error page, that the user gets shown
when a error has occurred within my website. The code works
perfectly, apart from when an invalid URL is typed in, then i am
unable to pass the exception from the global.asax file to the page via
the sessionstate. Everytime i try to pass something into the
sessionstate i get a error message of 'Object reference not set to an
instance of an object.'

Does anyone know how to get around this error? My code is below

GLOBAL.ASAX PAGE CODE

Sub Application_Err or(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs

Dim ctx As HttpContext = HttpContext.Cur rent
Dim LastException As Exception = ctx.Server.GetL astError
Dim Exception As New ExceptionItem(L astException)

' clear the error
ctx.Server.Clea rError()

HttpContext.Cur rent.Session("H ELLO") = "AAA"
HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)

Me.Response.Red irect("~/Errors/GeneralError.as px")
End Sub
CUSTOM ERROR PAGE CODE

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load

Dim Master As eContrack = Page.Master

Master.SectionT itle = "An error has occurred"

' Session("LastEx ception") is created in the Error event
of global.asax
Dim Exception As ExceptionItem =
CType(Session(" LastException") , ExceptionItem)

Exception.Inser t()

' See if errors should be sent by email
If
ConfigurationMa nager.AppSettin gs("SendErrorBy Email").ToStrin g = "true"
Then
Exception.Email (ConfigurationM anager.AppSetti ngs("SendErrorT oEmail"),
HelperMethod.Ge tDns)

End If

End Sub

Thanks in advance

Mar 12 '07 #1
10 2363
On Mar 12, 1:07 pm, "Nemisis" <darrens2...@ho tmail.comwrote:
Hi everyone,

I am trying to create a custom error page, that the user gets shown
when a error has occurred within my website. The code works
perfectly, apart from when an invalid URL is typed in, then i am
unable to pass the exception from the global.asax file to the page via
the sessionstate. Everytime i try to pass something into the
sessionstate i get a error message of 'Object reference not set to an
instance of an object.'

Does anyone know how to get around this error? My code is below

GLOBAL.ASAX PAGE CODE

Sub Application_Err or(ByVal sender As Object, ByVal e As
EventArgs)
' Fires when an error occurs

Dim ctx As HttpContext = HttpContext.Cur rent
Dim LastException As Exception = ctx.Server.GetL astError
Dim Exception As New ExceptionItem(L astException)

' clear the error
ctx.Server.Clea rError()

HttpContext.Cur rent.Session("H ELLO") = "AAA"
HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)

Me.Response.Red irect("~/Errors/GeneralError.as px")
End Sub

CUSTOM ERROR PAGE CODE

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load

Dim Master As eContrack = Page.Master

Master.SectionT itle = "An error has occurred"

' Session("LastEx ception") is created in the Error event
of global.asax
Dim Exception As ExceptionItem =
CType(Session(" LastException") , ExceptionItem)

Exception.Inser t()

' See if errors should be sent by email
If
ConfigurationMa nager.AppSettin gs("SendErrorBy Email").ToStrin g = "true"
Then

Exception.Email (ConfigurationM anager.AppSetti ngs("SendErrorT oEmail"),
HelperMethod.Ge tDns)

End If

End Sub

Thanks in advance
Please forgive me for being thickheaded; I get like that sometimes.
You're describing a NullReferenceEx ception. I'm not certain if it's
happening in global.asax or if it's happening in your custom error
page. I *think* it's happening in the custom error page.

If it's happening in the custom error page, its meaning will depend on
the line that the exception is occurring on. If it's happening on the
line where you're retreiving data from the session, I'd surmise that
it means that you either (a) don't have a session or (b) have a new
session and the value you're asking for isn't there.

However, I don't know what line the exception is being thrown from.
Could you be a little more specific?

Thanks!

Mar 12 '07 #2
Please forgive me for being thickheaded; I get like that sometimes.
You're describing a NullReferenceEx ception. I'm not certain if it's
happening in global.asax or if it's happening in your custom error
page. I *think* it's happening in the custom error page.

If it's happening in the custom error page, its meaning will depend on
the line that the exception is occurring on. If it's happening on the
line where you're retreiving data from the session, I'd surmise that
it means that you either (a) don't have a session or (b) have a new
session and the value you're asking for isn't there.

However, I don't know what line the exception is being thrown from.
Could you be a little more specific?
Sorry, i forgot to say where the error is occurring, Duh. lol.

The error is occurring when i try and set a session variable in the
global.asax file. The exact line where the error occurs is.

HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)

I have even tried writing it like

HttpContext.Cur rent.Session("L astException") = Exception
but that still doesnt work. I have checked that Exception actually
does have values set, and it does. And to even test it further, i
wrote in the following line of code to test setting a session
variable, and it still comes up with the same error.

HttpContext.Cur rent.Session("H ELLO") = "AAA"

I would really appreicate any help and insight as to why this is
happening. Thanks again

Mar 13 '07 #3
Sorry i forgot to write what line is causing the error Duh! :)

It is on the global.asax file when i am trying to set the value of a
session variable. The line is :

HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)

I have even tried to change it so it reads

HttpContext.Cur rent.Session("L astException") = Exception

but that still doesnt work. I do no that there is a value in
Exception, so i am not trying to store a null object. I even tried
the following line, and i am still getting the error.

HttpContext.Cur rent.Session("H ELLO") = "AAA"

I would really appreciate any insight to this problem. Cheers

Mar 13 '07 #4
Sorry i forgot to write what line is causing the error Duh! :)

It is on the global.asax file when i am trying to set the value of a
session variable. The line is :

HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)

I have even tried to change it so it reads

HttpContext.Cur rent.Session("L astException") = Exception

but that still doesnt work. I do no that there is a value in
Exception, so i am not trying to store a null object. I even tried
the following line, and i am still getting the error.

HttpContext.Cur rent.Session("H ELLO") = "AAA"

I would really appreciate any insight to this problem. Cheers

Mar 13 '07 #5
"Nemisis" <da*********@ho tmail.comwrote in message
news:11******** **************@ 30g2000cwc.goog legroups.com...
>Please forgive me for being thickheaded; I get like that sometimes.
You're describing a NullReferenceEx ception. I'm not certain if it's
happening in global.asax or if it's happening in your custom error
page. I *think* it's happening in the custom error page.

If it's happening in the custom error page, its meaning will depend on
the line that the exception is occurring on. If it's happening on the
line where you're retreiving data from the session, I'd surmise that
it means that you either (a) don't have a session or (b) have a new
session and the value you're asking for isn't there.

However, I don't know what line the exception is being thrown from.
Could you be a little more specific?

Sorry, i forgot to say where the error is occurring, Duh. lol.

The error is occurring when i try and set a session variable in the
global.asax file. The exact line where the error occurs is.

HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)
I bet that HttpContext.Cur rent.Session is Nothing, or else
HttpContext.Cur rent is Nothing. Check to see which.

John
Mar 13 '07 #6
On Mar 13, 5:13 am, "Nemisis" <darrens2...@ho tmail.comwrote:
Please forgive me for being thickheaded; I get like that sometimes.
You're describing a NullReferenceEx ception. I'm not certain if it's
happening in global.asax or if it's happening in your custom error
page. I *think* it's happening in the custom error page.
If it's happening in the custom error page, its meaning will depend on
the line that the exception is occurring on. If it's happening on the
line where you're retreiving data from the session, I'd surmise that
it means that you either (a) don't have a session or (b) have a new
session and the value you're asking for isn't there.
However, I don't know what line the exception is being thrown from.
Could you be a little more specific?

Sorry, i forgot to say where the error is occurring, Duh. lol.

The error is occurring when i try and set a session variable in the
global.asax file. The exact line where the error occurs is.

HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)

I have even tried writing it like

HttpContext.Cur rent.Session("L astException") = Exception

but that still doesnt work. I have checked that Exception actually
does have values set, and it does. And to even test it further, i
wrote in the following line of code to test setting a session
variable, and it still comes up with the same error.

HttpContext.Cur rent.Session("H ELLO") = "AAA"

I would really appreicate any help and insight as to why this is
happening. Thanks again
If the error is occurring there, then the problem is that you don't
have a valid session object. This is born out by the fact that you've
tried twice to write two different variables to the session, and both
of them raise the same exception.

So the next question I have for you is this: What is the exception
that is putting you in this exception handler in the first place?
Whatever it is, it's apparently occurring before you have a valid
session, or it's disconnecting the user from their session. I'd look
there.

Without a valid session, you won't be able to cache the exception
information in it. To prevent the NullReferenceEx ception, wrap the
offending statement in a conditional, as follows (assuming VB.NET):

If Not HttpContext.Cur rent.Session Is Nothing Then
HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)
End If

Hope this helps!

Mike

Mar 13 '07 #7
If the error is occurring there, then the problem is that you don't
have a valid session object. This is born out by the fact that you've
tried twice to write two different variables to the session, and both
of them raise the same exception.

So the next question I have for you is this: What is the exception
that is putting you in this exception handler in the first place?
Whatever it is, it's apparently occurring before you have a valid
session, or it's disconnecting the user from their session. I'd look
there.

Without a valid session, you won't be able to cache the exception
information in it. To prevent the NullReferenceEx ception, wrap the
offending statement in a conditional, as follows (assuming VB.NET):

If Not HttpContext.Cur rent.Session Is Nothing Then
HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)
End If

Hope this helps!

Mike
In order to get this error, i am trying to navigate to a page that
doesnt exist on my site ie. Home4.aspx. This causes the
application_Err or sub to be called, but the session is empty/null. If
i create a standard page, and write Throw New Exception(), again, the
application_err or code is called, but the session IS VALID, and i do
not get a NullReferenceEx ception.

Why do you get a nullreferenceex ception on a 404 error? Is it because
the page is not apart of your application, thus the session wont exist?

Mar 13 '07 #8
On Mar 13, 11:10 am, "Nemisis" <darrens2...@ho tmail.comwrote:
If the error is occurring there, then the problem is that you don't
have a valid session object. This is born out by the fact that you've
tried twice to write two different variables to the session, and both
of them raise the same exception.
So the next question I have for you is this: What is the exception
that is putting you in this exception handler in the first place?
Whatever it is, it's apparently occurring before you have a valid
session, or it's disconnecting the user from their session. I'd look
there.
Without a valid session, you won't be able to cache the exception
information in it. To prevent the NullReferenceEx ception, wrap the
offending statement in a conditional, as follows (assuming VB.NET):
If Not HttpContext.Cur rent.Session Is Nothing Then
HttpContext.Cur rent.Session.Ad d("LastExceptio n", Exception)
End If
Hope this helps!
Mike

In order to get this error, i am trying to navigate to a page that
doesnt exist on my site ie. Home4.aspx. This causes the
application_Err or sub to be called, but the session is empty/null. If
i create a standard page, and write Throw New Exception(), again, the
application_err or code is called, but the session IS VALID, and i do
not get a NullReferenceEx ception.

Why do you get a nullreferenceex ception on a 404 error? Is it because
the page is not apart of your application, thus the session wont exist?- Hide quoted text -

- Show quoted text -
Without seeing your code, I couldn't specifically answer your
question. :(

A 404 is generated for a number of reasons, however, and not always
because a page couldn't be found. (Microsoft might like you to believe
that a 404 is *always* raised for that reason, but you shouldn't just
take their word for it.)

First off, you know that the exception is occurring. I'd start by
addressing the exception. It sounds like you're trying to treat the
symptom (the fact that you don't have a session variable in your error
handler) instead of dealing with its cause (the fact that you're on
the error page to begin with).

Why are you even *on* this error page? How did you get here? What is
the condition that got you here? That's the defect that needs to be
resolved. Wrap that code in an exception handler. Don't rely on
global.asax to handle your exceptions. It's a FAILSAFE, not your main
exception handler.

Check this article out: http://aspnetresources.com/articles/...rrorPages.aspx

I'm not sure how familiar you are with this stuff, but it might shed
some light on the best practices for exception handling in ASP.NET.

Again, I hope I'm helping and not just confusing the snot out of ya.

Mike

Mar 13 '07 #9
Mike,

1. My code is at the top of the forum, u must of missed it.
2. I think i have not explained myself correctly, or you are not
getting what i mean. I can handle exceptions, that it fine, but i
just want to know why i cant get access to session variables when i am
in the application_err or sub, when i cause an error by directing to an
invalid/non existant page?
3. That is one of the articles i have read, it is a good article, and
as you can see my code is very similar
Mar 15 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
4959
by: | last post by:
Is it possible to access the previous page's viewstate directly through the context object after a server.transfer. If so, how? The way i'm doing it is saving all needed items to a structure on the previous page, then accessing its variables. I would like a much more direct way if one exists. thanks
1
5927
by: Greg Burns | last post by:
I am trying to write a global custom error page. I thought I would jot down some of what I've learned so far... At first I just used the default customErrors section with a defaultRedirect tag, as such: <customErrors defaultRedirect="error.aspx" mode="On" /> First thing I found out about this method, is that I didn't have access to the exception object in my error.aspx page. So this page was going to be
0
1079
by: Michael | last post by:
Hi, I have an ASP.NET application that catches un-handled exceptions in the Application_Error method and writes the details to the Event Log. But, if I do a Server.ClearError() followed by either a Server.Transfer or Response.Redirect to another ASP.NET page, then it causes .NET to re-enter the Application_Error function. I thought that maybe there is an exception being thrown in the Application_Error function, but after placing a...
0
980
by: Michael | last post by:
Hi, I have an ASP.NET application that catches un-handled exceptions in the Application_Error method and writes the details to the Event Log. But, if I do a Server.ClearError() followed by either a Server.Transfer or Response.Redirect to another ASP.NET page, then it causes .NET to re-enter the Application_Error function. I thought that maybe there is an exception being thrown in the Application_Error function, but after placing a...
2
2124
by: Carl Johansen | last post by:
I've been reading the O'Reilly "ASP.NET Cookbook" 1st edition (Kittel and LeBlond), and it makes a recommendation about exception handling that seems a bit strange. They say that, if you want to let some exceptions propagate up to Application_Error, you should include the following Page_Error on every page: Private Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Error Throw Server.GetLastError() End...
0
2266
by: Joergen Bech | last post by:
Fairly new to ASP.NET 1.1. Getting the error below when running application on a web server outside of my control, but only the first time I run it: 1. After a long period of inactivity (or updating the code-behind dll) accessing any aspx page in the application causes the application to run for the first time. Some of the initialization involves reading and writing some text and xml files using simple streamreader and streamwriter...
6
4570
by: Matt | last post by:
Can anyone give me a good reason to use BOTH application scope Page_Error and the page scope Page_Error when trapping errors in a web application? Is there any real benefit to using the Page_Error if you are already capturing errors in Application_Error? Also, if you have any links to discussions about when to use which function it would be helpful.
2
2131
by: Matt | last post by:
As the subject says... are Application_Error and Page_Error triggered by the same event? In other words, is there any error that could slip by Application_Error but be caught by Page_Error, or vise-versa? Or is it just a matter of handling the same exact thing in different scopes?
3
1959
by: Alex Maghen | last post by:
I want to create an object which is attached to the specific user session and I want to be able to access that object directly throughout the Pages, Page Controls, and Master Pages of the site. I'm assuming that the best way to do that is to create the object and then put it into the Session? If so, a few questions about the best way to do that: 1. If I want to reliably access that object on any and every page, how do I make sure that...
0
9489
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9298
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9906
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9885
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9737
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6562
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2698
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.