Solved

session variable / datetime

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 18
FTP file download using c# 3 24
Problem with SelectList in .NET MVC application 1 18
c# - Best approach for objects in functions 3 17
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

825 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