?
Solved

session variable / datetime

Posted on 2011-03-09
2
Medium Priority
?
358 Views
Last Modified: 2012-08-13
c sharp  .net

have a couple of Session Variables:

protected void Session_Start(object sender, EventArgs e)
{
    Session.Add("serverTimeAdjustment", 6);
    Session.Add("todaysDateTimeServerAdjusted", DateTime.Now.AddHours(Session["serverTimeAdjustment"]));
}

When I want to retieve this variable elsewhere in the web app, I use the following:

DateTime reportingDateTime = (DateTime)Session["todaysDateTimeServerAdjusted"];

Compiling is fine - but the debugger is then complaining when running ... and the following error on the above line:

'Object reference not set to an instance of an object.'
0
Comment
Question by:amillyard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 35082168
That would suggest that you are trying to read the session value at some point before it has been set. You should do a sanity check before trying to access it:
if (Session["todaysDateTimeServerAdjusted"] != null)
{
      DateTime reportingDateTime = (DateTime)Session["todaysDateTimeServerAdjusted"];
}
else
{
     // some sort of default
}

Open in new window

0
 

Author Closing Comment

by:amillyard
ID: 35082472
carl_tawn: spot on ... scripting was not initialising as early as hoped !  thanks :-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month11 days, 22 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question