Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "no implicit conversion between '<null>' and 'System.DateTime'" error message [closed]

In a previous question:

Getting "This method or property cannot be called on Null values" error

I had a problem with the following code:

client_group_details.Add(new ClientGroupDetails(
    reader.GetString(Col2Index),
    reader.GetString(Col3Index)));

Where I was getting the following error:

Data is Null. This method or property cannot be called on Null values.

This problem was resolved using the following code:

client_group_details.Add(new ClientGroupDetails(
    reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
    reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index)));

I now have a similar problem with GetDateTime and GetInt32, as example is:

client_group_details.Add(new ClientGroupDetails(
    reader.GetString(Col2Index),
    reader.GetString(Col3Index),
    reader.GetDateTime(Col4Index)));

I tried using the following to resolve this problem, but it didn't work

client_group_details.Add(new ClientGroupDetails(
    reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
    reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index),
    reader.IsDbNull(Col2Index) ? null : reader.GetDateTime(Col4Index)));

It gives the error:

Compiler Error Message: CS0173: Type of conditional expression cannot be determined because there is no implicit conversion between '<null>' and 'System.DateTime'

After searching for a solution, I found: Nullable type issue with ?: Conditional Operator. But when I try to use that code, I keep getting ) expected.

How would I resolve this problem?

like image 698
oshirowanen Avatar asked Dec 03 '22 05:12

oshirowanen


2 Answers

DateTime is a struct and therefore a value type and cannot be null. Only reference types and Nullable<T> (or T?) types can be null. You have to use a Nullable<DateTime>. This can also be written as DateTime?.

DateTime? dt = null;
dt = DateTime.Now;
if (dt.HasValue) ...
if (dt == null) ...
DateTime x = dt.Value;

dt = reader.IsDBNull(Col2Index) ? (DateTime?)null : reader.GetDateTime(Col4Index);
like image 87
Olivier Jacot-Descombes Avatar answered Dec 27 '22 13:12

Olivier Jacot-Descombes


You are missing a closing bracket somewhere.

client_group_details.Add(new ClientGroupDetails(
    reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
    reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index),
    reader.IsDbNull(Col2Index) ? null : reader.GetDateTime(Col4Index)));

Should probably change to

client_group_details.Add(new ClientGroupDetails(
    reader.IsDbNull(Col2Index) ? null : reader.GetString(Col2Index),
    reader.IsDbNull(Col3Index) ? null : reader.GetString(Col3Index),
    reader.IsDbNull(Col2Index) ? (DateTime?)null : reader.GetDateTime(Col4Index)));

or something similar. Depending on your exact code someone will be able to tell you where the missing bracket it.

like image 20
James Osborn Avatar answered Dec 27 '22 13:12

James Osborn