Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Operator '??' cannot be applied to operands of type 'System.DateTime'

I get the following error :

Operator '??' cannot be applied to operands of type 'System.DateTime'

 foreach (EndServReward r in reward)
                            {
                                if (con.State == ConnectionState.Closed)
                                {
                                    con.Open();
                                }
                                myIfxCmd.Parameters[0].Value = r.EmpNum ;
                                myIfxCmd.Parameters[1].Value = (r.ServDate) ?? DBNull.Value;
                            }

where reward is List<EndServReward> reward,why this happens ,and how to fix it ?

like image 488
Anyname Donotcare Avatar asked Oct 27 '13 14:10

Anyname Donotcare


2 Answers

?? is the null-coalescing operator.
It doesn't make sense to apply it to a value that cannot be null.

like image 62
SLaks Avatar answered Sep 21 '22 13:09

SLaks


The nullcoalescing operator cannot be applied by default on a type that is inherently non-nullable like DateTime. If you wish to use it anyway, you'll have to foresee using DateTime as a nullable type, using e.g. DateTime? dt;

like image 40
Wim Ombelets Avatar answered Sep 21 '22 13:09

Wim Ombelets