?
Solved

need help with nullable datetime in ASP.NET 2.0

Posted on 2007-03-19
5
Medium Priority
?
957 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
[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
  • 3
  • 2
5 Comments
 
LVL 13

Expert Comment

by:Webstorm
ID: 18748984
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
ID: 18749139
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
ID: 18749190
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 1000 total points
ID: 18749400
Then use the following :
u.ModifiedOn = Convert.IsDBNull(user["ModifiedOn"]) ? (DateTime)null : Convert.ToDateTime(user["ModifiedOn"]);
0
 

Author Comment

by:IUAATech
ID: 18749525
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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
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, 11 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