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

Session_End event, System.NullReferenceException

P: n/a
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.ConnectionStrin gs["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


Feb 16 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
you are not validating that the session data (LogId) exists before using it.

-- bruce (sqlwork.com)

brent960 wrote:
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.ConnectionStrin gs["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

Feb 17 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.