Solved

need help with nullable datetime in ASP.NET 2.0

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

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…
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…
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

733 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