Solved

need help with nullable datetime in ASP.NET 2.0

Posted on 2007-03-19
5
952 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 250 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

943 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

7 Experts available now in Live!

Get 1:1 Help Now