Solved

session variable / datetime

Posted on 2011-03-09
2
350 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 500 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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

688 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