473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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 6880
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Kenn Ghannon | last post by:
I've got an ASP.NET page with a counter subtraction routine in the Session_End method in the Global.asax.cs: protected void Session_End(Object sender, EventArgs e) { ulong curUsers; ...
5
by: Steve M | last post by:
Why are my sessions lasting so long? I have them set to 20 minute timeout in config file? The Session_End event is getting called an hour or more sometimes--well after the user has stopped...
0
by: Savin Igor | last post by:
What can be done in Session_End()? Can I get something from Session in this handler? My Global.asax.cs is below using System; using System.Collections; using System.ComponentModel; using...
6
by: Beren | last post by:
Hey, I've got some code in my Session_End which cleans up all Session related items from the Cache object. I needed to store some related sessiondata in there to be able to address it in the...
1
by: Timo | last post by:
A few questions relating to sessions ending. 1. What happens to the session when a user closes the browser? Does the session remain alive until it times out? 2. In the Session_End...
11
by: OldProgrammer | last post by:
All the documentation and discussion I have read indicate that the Session_End is not supposed to fire unless you are in "inProc" Session state mode, and then only on Session Timeout or at Session...
8
by: Roger | last post by:
When I call the session.abandon() method, it calls the session_end event. When a user closes the browser or clicks the log off button, I can dispose of objects and abandon the session cleaning....
12
by: =?Utf-8?B?QWxleCBNYWdoZW4=?= | last post by:
Hi. I am trying to maintain a list of people who are currently "online" in SQL. I do this by adding a simple entry to a simple PeopleOnline table whenever someone logs in to my site. If they...
1
by: anithaapr05 | last post by:
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: void...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.