Sayamima
Sayamima

Reputation: 225

handling null values of datetime

I have a Linq query , which consists of this datetime field i'm extracting from database

           TerminationDate = base.ConvertFromUtcToCentral(r.TerminationDate.ToString())

the function used is designed like this

           public DateTime ConvertFromUtcToCentral(string UtcTime)
    {

        if (UtcTime != "")
        {
            DateTime?  retValue = TimeZoneInfo.ConvertTimeFromUtc(Convert.ToDateTime
                                                       (UtcTime), USTimeZones.Central);

            return Convert.ToDateTime(retValue);
        }
        else
            return Convert.ToDateTime(null);

    }

But whenever i return null it shows up the min date in the grid that i assign this query to. However, when it is null, it should just showup null instead of min date. can you please let me know how i can handle this?Also let me knoww if using ternary operator is a better idea.

Restrictions: Can't change the function retrun type as it is used in several places

Upvotes: 0

Views: 9553

Answers (5)

Appyks
Appyks

Reputation: 496

Can’t assign null value to DateTime since it’s not a nullable type Try assigning DBNull.Value instead of null while posting it back to database

Upvotes: 0

Gonzalo.-
Gonzalo.-

Reputation: 12682

maybe you can try to return null. Make your function for return DateTime?, so instead of return Convert.ToDateTime(null); you can do return null

sorry for the bad english!

Upvotes: 2

hangy
hangy

Reputation: 10859

The recent edits and comments suggest that TerminationDate probably is DateTime?. Maybe adding a new overload that explicitly handles DateTime and DateTime values and correctly returns DateTime or DateTime? instead? I see that you cannot change the return type of this method easily, so adding those new overloads and gradually using them in your application could be a good approach towards to using a better data type.

public DateTime ConvertFromUtcToCentral(string UtcTime)
{
    return !string.IsNullOrEmpty(UtcTime)
        ? this.ConvertFromUtcToCentral(Convert.ToDateTime(UtcTime))
        : default(DateTime);
}

public DateTime? ConvertFromUtcToCentral(DateTime? UtcTime)
{
    return UtcTime.HasValue
        ? this.ConvertFromUtcToCentral(UtcTime.Value)
        : (DateTime?)null;
}

public DateTime ConvertFromUtcToCentral(DateTime UtcTime)
{
    return TimeZoneInfo.ConvertTimeFromUtc(UtcTime, USTimeZones.Central);
}

If you do not change return type, you will always need get a non-null DateTime value from the method, unless - as user957902 proposed - you begin throwing Exceptions. :) In the the case that you cannot use this, you might also modify the consuming application to treat DateTime.MinValue like null and do not display anything.

Using the ternary operator or not can be a matter of taste - in simple cases like these, I do like using it (the line breaks in my example are purely to make it easier to read on SO), because it is pretty easy to read and there are only two paths to be taken canyways.

Upvotes: 1

user957902
user957902

Reputation: 3060

With the restriction that you can't change the function then you could throw a terinery operator in:

TerminationDate = ( base.ConvertFromUtcToCentral(r.TerminationDate.ToString()) == default(DateTime) ? (DateTime?) null : (DateTime ?) base.ConvertFromUtcToCentral(r.TerminationDate.ToString()) );

I don't like this as it call the function twice. I would probably write a wrapper method that did something like this

public static DateTime? WrapperConvertFromUtcToCentral(string UtcTime)
{
    DateTime value = ConvertFromUtcToCentral(string UtcTime);

    if (value == default(DateTime))
         return (DateTime?)null;
    else
         return (DateTime?)value;
}

TerminationDate = WrapperConvertFromUtcToCentral(r.TerminationDate.ToString())        

This way you are calling the orignal function only once.

Upvotes: 1

user957902
user957902

Reputation: 3060

Since you are returning a value type, DateTime, you can not return null. You can either change your function to return a nullible DateTime (DateTime?), throw an exception, or return the default value for DateTime, default(DateTime). IMHO I would throw a System.ArgumentException since an empty string to convert is clearly not going to produce a output.

Upvotes: 3

Related Questions