2. In the Session_Start procedure in the Global.asax file I set the session variable to a default value.
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
' Fires when the session is started
HttpContext.Current.Session("ErrorTicketID") = "0"
End Sub
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Fires when an unhandled error occurs 'Get the next ErrorTicketId and save in Session variable If Not HttpContext.Current.Session Is Nothing Then Dim intLastError As String = (UDF_GetSPValue(20096)).ToString HttpContext.Current.Session("ErrorTicketID") = intLastError.ToString Else MsgBox("No Session") End If
Dim clsLogError As New CLS_Error_Handling(Server.GetLastError, Request) clsLogError.LogError() End Sub
5. To check that the session is a valid I added the following code to the Page On Load event and it returns the session variable correctly.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim intLastError As String = (UDF_GetSPValue(20096)).ToString
HttpContext.Current.Session("ErrorTicketID") = intLastError.ToString
Me.txttest.Text = HttpContext.Current.Session("ErrorTicketID")
End Sub
My problem seems to be that when the Application_Error procedure is executed the session state is lost.
Does anyone know why the session state isn't available in the Global.asax file.
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
' Fires when an unhandled error occurs
'Get the next ErrorTicketId and save in Session variable
If Not HttpContext.Current.Session("ErrorTicketID") Is Nothing Then
Dim intLastError As String = (UDF_GetSPValue(20096)).ToString
HttpContext.Current.Session("ErrorTicketID") = intLastError.ToString
Else
MsgBox("No Session")
End If
Dim clsLogError As New CLS_Error_Handling(Server.GetLastError, Request)
clsLogError.LogError()
End Sub
If your application is throwing something like HTTP 404 (File Not Found) error, the Session is always null. Reason being the HttpContext object is created by ASP.NET during the page request. Since no page was found, no HttpContext Object can be created;
therefore, no Session can exist when a 404 error occurs.
-Sekhar
Please mark the replies as answers if they help or unmark if not.
Thank You Bala, this was in fact my problem. To create an error I intentionally pointed to a page that didn't exist. Once I tested using a different type of error the session variable was available in my Global.asax file.
But, as I move on through the process I am now having the same problem in a different area. I think the cause is probably the same problem, I just don't know how to fix it. Possibly you know a solution?
Here is the scenario;
1. I have created a custom error page that I have setup in the web.config file using the following;
2. When an error occurs the Application_Error procedure in the Global.asax file is executed. In this procedure I get the next TicketId from the database and assign it to a session variable ErrorTicketId . I verified that the session variable now holds
the ErrorTicket Id.
I then call another procedure that logs the error to a database table with the ErrorTicketId. Again, this process works fine.
3. The next step is that my custom error page opens. When the custom error page opens I would like to use the value in the session variable ErrorTicketId to display a message to the user, like
An Application Error Has Occurred
Your ticket number for this is error is Session("ErrorTicketId"). The IT department has been notified of this error and will respond as soon as possible.
My problem is when the custom error page opens the session is null. I put the following code on the Page_Load event and I always get the message box No Session
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not HttpContext.Current.Session Is Nothing Then
Dim strErrorTicketId As String = HttpContext.Current.Session("ErrorTicketId").ToString
Else
MsgBox("No Session")
End If
End Sub
Do you know how I can retain the value of the ErrorTicketId so that I can display it on the custom error page?
Could you please try opening the "WBF Error Page.aspx" file from the browser directly?
Correction in your code:Don't type cast a session variable directly to string without checking for null.
To answer your question how we can retain the ErrorTicketId, pass it as a query string parameter to the errro page. I doubt you can pass it as a query string param if you do custom error configuration with redirect.
Alternatively, add Application_Error event handler in the global.asax file. And the code to redirect to the page with TicketId in query string params.
-Sekhar
Please mark the replies as answers if they help or unmark if not.
Marked as answer by trafine on Feb 04, 2011 11:54 PM
trafine
Member
8 Points
10 Posts
Losing Session State In Global.asax file
Feb 02, 2011 12:44 AM|LINK
Hi,
I am trying to set a session variable in the global.asax file and keep running into the problem that the Session is null.
I am running my application in VS2008 and using asp.net 3.51.
Here is my code:
1. In my web.config file I have enabled the session state and added the following line;
<sessionState cookieless="false" mode="InProc" timeout="60" ></sessionState>
2. In the Session_Start procedure in the Global.asax file I set the session variable to a default value.
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs) ' Fires when the session is started HttpContext.Current.Session("ErrorTicketID") = "0" End Sub
session start asp.net. session asp.net session application variables
RDO
Member
309 Points
282 Posts
Re: Losing Session State In Global.asax file
Feb 02, 2011 02:55 AM|LINK
Try
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs) ' Fires when an unhandled error occurs 'Get the next ErrorTicketId and save in Session variable If Not HttpContext.Current.Session("ErrorTicketID") Is Nothing Then Dim intLastError As String = (UDF_GetSPValue(20096)).ToString HttpContext.Current.Session("ErrorTicketID") = intLastError.ToString Else MsgBox("No Session") End If Dim clsLogError As New CLS_Error_Handling(Server.GetLastError, Request) clsLogError.LogError() End SubChristian Forum
bala.sekhar
Member
294 Points
62 Posts
Re: Losing Session State In Global.asax file
Feb 02, 2011 04:01 AM|LINK
Please check, this might be a reason.
If your application is throwing something like HTTP 404 (File Not Found) error, the Session is always null. Reason being the HttpContext object is created by ASP.NET during the page request. Since no page was found, no HttpContext Object can be created; therefore, no Session can exist when a 404 error occurs.
Please mark the replies as answers if they help or unmark if not.
KumarHarsh
All-Star
15133 Points
3647 Posts
Re: Losing Session State In Global.asax file
Feb 02, 2011 06:00 AM|LINK
in Sub Application_Error,
you can try Server.GetLastError()
I hope it solve the purpose.
Kumar Harsh
trafine
Member
8 Points
10 Posts
Re: Losing Session State In Global.asax file
Feb 02, 2011 09:12 PM|LINK
Thank You Bala, this was in fact my problem. To create an error I intentionally pointed to a page that didn't exist. Once I tested using a different type of error the session variable was available in my Global.asax file.
But, as I move on through the process I am now having the same problem in a different area. I think the cause is probably the same problem, I just don't know how to fix it. Possibly you know a solution?
Here is the scenario;
1. I have created a custom error page that I have setup in the web.config file using the following;
<customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="WBF Error Page.aspx"/>
2. When an error occurs the Application_Error procedure in the Global.asax file is executed. In this procedure I get the next TicketId from the database and assign it to a session variable ErrorTicketId . I verified that the session variable now holds the ErrorTicket Id.
I then call another procedure that logs the error to a database table with the ErrorTicketId. Again, this process works fine.
3. The next step is that my custom error page opens. When the custom error page opens I would like to use the value in the session variable ErrorTicketId to display a message to the user, like
An Application Error Has Occurred
Your ticket number for this is error is Session("ErrorTicketId"). The IT department has been notified of this error and will respond as soon as possible.
My problem is when the custom error page opens the session is null. I put the following code on the Page_Load event and I always get the message box No Session
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not HttpContext.Current.Session Is Nothing Then Dim strErrorTicketId As String = HttpContext.Current.Session("ErrorTicketId").ToString Else MsgBox("No Session") End If End SubThank you for your help.
Jerry Weng -...
All-Star
29527 Points
3488 Posts
Re: Losing Session State In Global.asax file
Feb 04, 2011 04:42 AM|LINK
Hi,
To display safe error message, please follow this document:
http://msdn.microsoft.com/en-us/library/994a1482.aspx
bala.sekhar
Member
294 Points
62 Posts
Re: Losing Session State In Global.asax file
Feb 04, 2011 09:55 AM|LINK
Could you please try opening the "WBF Error Page.aspx" file from the browser directly?
Correction in your code:Don't type cast a session variable directly to string without checking for null.
To answer your question how we can retain the ErrorTicketId, pass it as a query string parameter to the errro page. I doubt you can pass it as a query string param if you do custom error configuration with redirect.
Alternatively, add Application_Error event handler in the global.asax file. And the code to redirect to the page with TicketId in query string params.
Please mark the replies as answers if they help or unmark if not.
trafine
Member
8 Points
10 Posts
Re: Losing Session State In Global.asax file
Feb 05, 2011 12:00 AM|LINK
Thank you Jerry for this link it was helpful.
trafine
Member
8 Points
10 Posts
Re: Losing Session State In Global.asax file
Feb 05, 2011 12:01 AM|LINK
Thank you Bala
I am going to take out the custom error handling in the web config and use the server.transfer in the Global.asax file.
KumarHarsh
All-Star
15133 Points
3647 Posts
Re: Losing Session State In Global.asax file
Feb 05, 2011 06:00 AM|LINK
Sorry to say you didn't give importance to my post.
I already said ,
Kumar Harsh