470,573 Members | 1,761 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,573 developers. It's quick & easy.

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.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
1 6694
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.

Similar topics

9 posts views Thread by Kenn Ghannon | last post: by
reply views Thread by Savin Igor | last post: by
6 posts views Thread by Beren | last post: by
1 post views Thread by Timo | last post: by
11 posts views Thread by OldProgrammer | last post: by
8 posts views Thread by Roger | last post: by
12 posts views Thread by =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post: by
1 post views Thread by anithaapr05 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.