[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

session variable / datetime

Posted on 2011-03-09
2
Medium Priority
?
364 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
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 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