Session_End event, System.NullReferenceException



Environment: ASP.NET 2.0, SQL Server 2005, C#, Visual Studio 2005

In my Session_End event, I am executing a stored procedure to update a
database table that is used to log user sessions.

When the user sessions time out, the Session_End event fires successfully.
The stored procedure executes successfully, and I can see the updated data in
the database just as I would expect.

Nonetheless, an exception is being generated by the code that I'ved added to
the Session_End event. This is what part of the exception looks like when
it's emailed to me from the ASP.NET Health Monitoring service:

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

Thread information:
Thread ID: 1
Thread account name: NT AUTHORITY\NETWORK SERVICE
Is impersonating: False
Stack trace: at ASP.global_asax.Session_End(Object sender, EventArgs e)
=====

And here is the code that of my Session_End event handler:

=====
void Session_End(object sender, EventArgs e)
{
try
{
SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString);
SqlCommand cmd = new
SqlCommand("FinalizeAuthenticatedUserSession", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;

// Add the parameters
// The LogID for this log entry is stored in the session variable
cmd.Parameters.Add(new SqlParameter("@LogID", SqlDbType.Int, 4));
cmd.Parameters["@LogID"].Value = (int)Session["LogID"];

// @SessionEndDateTime is expressed in UTC
cmd.Parameters.Add(new SqlParameter("@SessionEndDateTime",
SqlDbType.DateTime, 8));
cmd.Parameters["@SessionEndDateTime"].Value = DateTime.UtcNow;
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException err)
{
throw new ApplicationException("A database exception
occurred while updating the authenticated user's record in the database in
the Session_End event handler: " + err.Message);
}
finally
{
con.Close();
}
}
catch (Exception err)
{
throw new ApplicationException("An exception occurred in the
Session_End event handler: " + err.Message);
}
}
=====

Even though the database is being updated correctly, I'm overwhelmed with
error messages from the Health Monitoring service.

If I had to venture a guess, I'd say that one of the resources that is being
cleaned up after the execution of the stored procedure cannot be cleaned up,
due to the session ending.

Yes, I've tried debugging this, and I have not been successful in stepping
through an execution of the code that led to the exception.

Any assistance anyone could provide here would be welcome.

-- brent




.



Relevant Pages

  • Re: error in accessing database
    ... Access requires the creation of an .ldb file when the database ... An unhandled exception occurred during the execution of the ... current web request. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: StoredProc commits results in QueryAnalyzer but not in ADO.NET
    ... I have a stored procedure in an MSDE database. ... Analyser to run this stored procedure the correct results appear in the ... catch (Exception exception) ... my parameters and my command and connection objects are created in the ...
    (microsoft.public.dotnet.framework.adonet)
  • Re: stored procedure occassionally running 10x slower???
    ... > related to the database usage. ... > Using SQL Profiler (filtered to trace the stored procedure events), ... > anomalies and normal execution). ... > select the Show Plan All I will get this event ...
    (microsoft.public.sqlserver.programming)
  • Re: Session_End event, System.NullReferenceException
    ... you are not validating that the session data exists before using it. ... I am executing a stored procedure to update a database table that is used to log user sessions. ... an exception is being generated by the code that I'ved added to the Session_End event. ... If I had to venture a guess, I'd say that one of the resources that is being cleaned up after the execution of the stored procedure cannot be cleaned up, due to the session ending. ...
    (microsoft.public.dotnet.framework.aspnet)
  • StoredProc commits results in QueryAnalyzer but not in ADO.NET
    ... I have a stored procedure in an MSDE database. ... Analyser to run this stored procedure the correct results appear in the ... catch (Exception exception) ... my parameters and my command and connection objects are created in the ...
    (microsoft.public.dotnet.framework.adonet)