getting error while date is null . error line- DateTime renewalDate = row.Field("RenewalDate");
protected void GrdV_Projects_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow )
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
DateTime renewalDate = row.Field<DateTime>("RenewalDate");
if (renewalDate.Date > DateTime.Today)
e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#669B1F");
else
e.Row.Cells[7].BackColor = System.Drawing.ColorTranslator.FromHtml("#FF8234");
}
}
}
you cant convert null to date, make date-time nullable
DateTime? renewalDate = row.Field<DateTime?>("RenewalDate")
also in your if statement
if (renewalDate.HasValue && renewalDate.Value.Date > DateTime.Today)
The error is pretty clear I think. You can't assign a NULL value to a DateTime
. You have two options:
NOT NULL
in the DB to ensure it cannot return NULL
. Use a DateTime?
instead of DateTime
:
DateTime? renewalDate = row.Field<DateTime?>("RenewalDate");
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