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?

Brian Var
  • 6,029
  • 25
  • 114
  • 212
  • 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? – Steve Oct 13 '16 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. – Brian Var Oct 13 '16 at 19:59

2 Answers2

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.

Paweł Hemperek
  • 1,130
  • 10
  • 21
  • 1
    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 :-) – Paweł Hemperek Oct 13 '16 at 20:03
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") ?? "";
Ivan Stoev
  • 195,425
  • 15
  • 312
  • 343
  • Good idea, didn't you mean `.Value.ToString("d-MMMM-yyyy") ?? "",` to string though? – Brian Var Oct 14 '16 at 07:56
  • 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. – Brian Var Oct 14 '16 at 08:06
  • 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). – Ivan Stoev Oct 14 '16 at 08:49