On Feb 14, 2:09 pm, "Bill Gower" <billgo...@charter.netwrote:
Why won't this work? What do I need to do to make it work?
DateTime? DateMember;
if((DateTime.Parse(oldRow["datemember"].ToString) == null))
DateMember = null;
else
DateMember = DateTime.Parse(oldRow["datemember"].ToString());
The program is failing on the if saying that the null value in the field in
the table is not able to be represented as a string. Which is fine, I can
understand that, so how do I check for a field in a table for null and
assign either a null to a local variable to the field value.
Bill
The above won't work because: oldRow["datemember"].ToString() will
fail if oldRow["datemember"] is null.
Test for if your datetime field equals to DBNull.Value:
if (oldRow["datemember"] != DBNull.Value)
DateMember = DateTime.Parse(oldRow["datemember"].ToString());
else
DateMember = DateTime.MinValue; //You cannot assign a DateTime
value to null
One liner:
DateMember = oldRow["datemember"] == DBNull.Value ?
DateTime.MinValue :
DateTime.Parse(oldRow["datemember"].ToString());
Have fun.
Quoc Linh