My ProcessDate type is DateTime?
when i use ToString it is showing that exception.
dfCalPlanDate.Text = concreteCalPlan.ProcessDate.ToString("d");
Thank you for your interested.
Simple: Nullable<T>
(and, thus, Nullable<DateTime>
, aka DateTime?
) does not have a method ToString(String)
.
You probably wanted to invoke DateTime.ToString(String)
. To do this in a null-safe way, you can use C# 6's null-conditional operator ?.
:
dfCalPlanDate.Text = concreteCalPlan.ProcessDate?.ToString("d");
which is a concise way of writing:
var date = concreteCalPlan.ProcessDate;
dfCalPlanDate.Text = (date == null ? null : date.Value.ToString("d"));
Note that this will yield null
if ProcessDate
is null
. You can append the null-coalescing operator ??
if you need another result in that case:
dfCalPlanDate.Text = concreteCalPlan.ProcessDate?.ToString("d") ?? "no date set";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With