Session_End event, System.NullReferenceException

G

Guest

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
 
B

bruce barker

you are not validating that the session data (LogId) exists before using it.

-- bruce (sqlwork.com)
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top