1

I'm reading back a nullable datetime using the IDataReader interface. So far my previous column reads are working as expected.

Except for this column ["Implementation End Timestamp"] where I try to read back a nullable date time, convert it to string and assign it to a string property named Implementation_End_String.

So this is what I tried. First Reading back the DateTime? value checking for null then attempting a convert toString() if not null.

But this assignment isn't allowed due to their being "no explicit conversion between string and DateTime?":

Implementation_End_String = dataReader["Implementation End Timestamp"] == DBNull.Value ? (DateTime?)null : Convert.ToDateTime(dataReader["Implementation End Timestamp"]).ToString("d-MMMM-yyyy"), //show full month name

I think I need to get the value of the DateTime to call toString() on it.

Question:

How can I convert the read back DateTime? value to a string type?

2
  • If the field is supposed to contain a DateTime why do you want to convert it to a string and then back to a DateTime? Commented Oct 13, 2016 at 19:56
  • No Implementation_End_String is a string property, I want to convert the read back ["Implementation End Timestamp"]date to a string representation in short month format. Then assign it to the string variable If that makes more sense. Commented Oct 13, 2016 at 19:59

2 Answers 2

3

Every Nullable<T> type has GetValueOrDefault method. You can use this method to retrieve value or default value of T (in your case it would be DateTime.MinValue) if there is no value. This method will return plain DateTime object so you can invoke any ToString() methods on it.

Sign up to request clarification or add additional context in comments.

1 Comment

As I said - it's good to read the docs! GetValueOrDefault returns default value of type T so in your case it would be date 01/01/0001 00:00:00 as this is default DateTime value. Therefore you should not check if it is not null, but if it is not DateTime.MinValue. Sorry for the confusion! I updated answer in case someone would Google this question :-)
3

IDataReader is a quite old interface, thus does not support natively nullable types. If you tend to use it at many places in your code, you'd better create some helpers which will significantly reduce your code. For instance, here are the helper methods for DateTime?, you could easily do similar for the other types:

public static class DataReaderExtensions
{
    public static DateTime? GetNullableDateTime(this IDataReader source, string name)
    {
        return source.GetNullableDateTime(source.GetOrdinal(name));
    }
    public static DateTime? GetNullableDateTime(this IDataReader source, int i)
    {
        return !source.IsDBNull(i) ? source.GetDateTime(i) : (DateTime?)null;
    }
}

This, in combination with the C#6 null conditional operator would make the task in question simple as that:

Implementation_End_String = dataReader
    .GetNullableDateTime("Implementation End Timestamp")?.ToString("d-MMMM-yyyy") ?? "";

3 Comments

Good idea, didn't you mean .Value.ToString("d-MMMM-yyyy") ?? "", to string though?
Although using .value before toString, gives "Nullable object must have a value." error. I did also try ?.ToString("d-MMMM-yyyy") ?? "", but get a syntax error on that assignment.
No, it shouldn't be .Value. If you are getting syntax error, then you probably are not using VS2015 (note the C#6 in the answer).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.