By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,180 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,180 IT Pros & Developers. It's quick & easy.

Warning in Session_End()

P: 11
After the session end i try to write the session value.In this, it write the session value after the session end, but i got the warning in application event.

My asp code:
Expand|Select|Wrap|Line Numbers
  1. void Session_End(object sender, EventArgs e)
  2.     {
  3.         // Code that runs when a session ends. 
  4.  
  5.         // Note: The Session_End event is raised only when the sessionstate mode
  6.         // is set to InProc in the Web.config file. If session mode is set to StateServer
  7.         // or SQLServer, the event is not raised.        
  8.  
  9.         string domainName = AppDomain.CurrentDomain.BaseDirectory.ToString();
  10.         string strDirName = domainName.Replace("bin\\Debug\\", "");
  11.  
  12.         string enableLogging = ConfigurationSettings.AppSettings.Get("EnableSessionLog");
  13.  
  14.         if (enableLogging.ToLower() == "true")
  15.         {
  16.  
  17.             TextWriter tw = new StreamWriter(strDirName + "Log Files\\SessionLog.txt", true);
  18.             if (Session["portalID"] != null)
  19.             {
  20.                 tw.WriteLine("Session Ends");
  21.                 tw.WriteLine(DateTime.Now + "  SessionTimedout SessionID = " + Session.SessionID);
  22.                 tw.WriteLine(DateTime.Now + "  SessionTimedout UserName = " + Session["session_username"].ToString());
  23.                 tw.WriteLine(DateTime.Now + "  SessionTimedout PortalID = " + Session["portalID"].ToString());
  24.                 tw.WriteLine(DateTime.Now + "  SessionTimedout PatientID = " + Session["patientID"].ToString());
  25.             }
  26.             tw.Close();
  27.  
  28.         }
  29.     }    
My Warning is:

Event code: 3005
Event message: An unhandled exception has occurred.
Event time: 8/20/2009 11:19:24 AM
Event time (UTC): 8/20/2009 5:49:24 AM
Event ID: 7d1169968dd94902bc75c75234933e12
Event sequence: 968
Event occurrence: 1
Event detail code: 0

Application information:
Application domain: b42109ff-1-128952191043738926
Trust level: Full
Application Virtual Path: /ApplicationName
Application Path: C:\ApplicationName\
Machine name: MachineName

Process information:
Process ID: 3576
Process name: WebDev.WebServer.EXE
Account name: MachineName\Administrator

Exception information:
Exception type: NullReferenceException
Exception message: Object reference not set to an instance of an object.

Request information:
Request URL:
Request path:
User host address:
User:
Is authenticated: False
Authentication Type:
Thread account name: MachineName\Administrator

Thread information:
Thread ID: 10
Thread account name: MachineName\Administrator
Is impersonating: False
Stack trace: at ASP.global_asax.Session_End(Object sender, EventArgs e) in c:\ApplicationName\Global.asax:line 80

I got the warning in this line- tw.WriteLine(DateTime.Now + " SessionTimedout UserName = " + Session["session_username"].ToString());

After session end i tried to write the session value.Please any one tell me how to avoid this
Sep 1 '09 #1
Share this Question
Share on Google+
1 Reply


Frinavale
Expert Mod 5K+
P: 9,731
@anithaapr05
Always check to see if the values in session are null before trying to use them. It seems that you are attempting to access Objects that no longer exist. First check if they exist, and if they do exist then use them. Don't ever assume that they exist...

It should look something like:
Expand|Select|Wrap|Line Numbers
  1.    if (enableLogging.ToLower() == "true")
  2.    {
  3.  
  4.             TextWriter tw = new StreamWriter(strDirName + "Log Files\\SessionLog.txt", true);
  5.             if (Session["portalID"] != null)
  6.             {
  7.                 tw.WriteLine("Session Ends");
  8.                 if(Session != null){
  9.                   If(Session.SessionID != null){
  10.                     tw.WriteLine(DateTime.Now + "  SessionTimedout SessionID = " + Session.SessionID);
  11.                   }
  12.                  }
  13.                 If(Session["session_username] != null){
  14.                   tw.WriteLine(DateTime.Now + "  SessionTimedout UserName = " + Session["session_username"].ToString());
  15.                 }
  16.                 if(Session["portalID"] != null){
  17.                   tw.WriteLine(DateTime.Now + "  SessionTimedout PortalID = " + Session["portalID"].ToString());
  18.                 }
  19.                 if(Session["patientID"] != null){
  20.                   tw.WriteLine(DateTime.Now + "  SessionTimedout PatientID = " + Session["patientID"].ToString());
  21.                 }
  22.             }
  23.             tw.Close();
  24.     }
  25.  
Sep 1 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.