?
Solved

need help with nullable datetime in ASP.NET 2.0

Posted on 2007-03-19
5
Medium Priority
?
961 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
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

850 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