Solved

need help with nullable datetime in ASP.NET 2.0

Posted on 2007-03-19
5
950 Views
Last Modified: 2011-10-03
I am trying to take advantage of the nullable datetime introduced in C# 2005 to handle null datetime values I have in my database. here is my approach.

My "DB class" has the following property:

   private DateTime? _modifiedOn; // nullable type since _modifiedOn can sometimes be null

    public DateTime? ModifiedOn
    {
        get
        {
            return _modifiedOn;
        }
        set
        {
            _modifiedOn = value;
        }
    }

in my Business Layer Logic, I have the following:
    public static UserProfile GetUserProfile(int UserId)
    {
        UserProfileDAL.UsersDataTable udt = usersAdapter.GetUserProfile(UserId);
        UserProfile u = new UserProfile();

        if (udt.Count > 0)
        {
            UserProfileDAL.UsersRow user = udt[0];

            u.ModifiedOn = Convert.ToDateTime((Convert.IsDBNull(user["ModifiedOn"])) ? null : user["ModifiedOn"]);
             // ....and other stuff
        }

        return u;
    }

Now, in my presentation layer, when I try to do something like the following:

lbUpdatedOn.Text = (up.ModifiedOn.HasValue) ? up.ModifiedOn.Value.ToShortDateString() : "N/A";

I get 1/1/0001 instead of "N/A" despite the value being NULL in the databse.

What am I doing wrong here? Looks like my  understanding of nullable datatype is flawed.

Any suggestion would be really helpdful.
0
Comment
Question by:IUAATech
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Webstorm
Comment Utility
Hi IUAATech,

Check your database : does this field has a default value (1/1/0001 for example) ? can this field be null (NULL in the field declaration) ?

Also, instead of
u.ModifiedOn = Convert.ToDateTime((Convert.IsDBNull(user["ModifiedOn"])) ? null : user["ModifiedOn"]);

use the following :
u.ModifiedOn = Convert.IsDBNull(user["ModifiedOn"]) ? null : Convert.ToDateTime(user["ModifiedOn"]);

Convert.ToDateTime(null) may return a default datetime
0
 

Author Comment

by:IUAATech
Comment Utility
Webstorm,
the field has no default value. And yes, this field can be null.

I tried your code and I get the following compilation error:

Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'

any ideas?
0
 

Author Comment

by:IUAATech
Comment Utility
looks like we have others facing the same problem:

http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_22124183.html
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 250 total points
Comment Utility
Then use the following :
u.ModifiedOn = Convert.IsDBNull(user["ModifiedOn"]) ? (DateTime)null : Convert.ToDateTime(user["ModifiedOn"]);
0
 

Author Comment

by:IUAATech
Comment Utility
I get a the following error:
Cannot convert null to 'System.DateTime' because it is a value type

However, u.ModifiedOn = Convert.IsDBNull(user["ModifiedOn"]) ? (DateTime?)null : Convert.ToDateTime(user["ModifiedOn"]);
works!

Thanks a bunch.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now