So, First of all. Code:
I’ve got a class:
public class Myobject
{
public string Code { get; set; }
public DateTime? StartDate { get; set; }
}
And this is part of very simple source:
MyObject mo = new MyObject();
mo.Code= "sth";
// NO action on StartDate property!
if (mo.StartDate.HasValue)
{
sc.Parameters.Add(new SqlParameter("@inStartDate", mo.StartDate.Value));
}
else
{
sc.Parameters.Add(new SqlParameter("@inStartDate", DBNull.Value));
}
Simple ‘if’ – Sql Server 2008, throw an error – when gets null Datetime (it has to be DBNull.Value)
So I want to check it first, and then pass right value or DBNull.
My problem is – this ‘if’ always retruns true! Why!?
Also tried that:
if (mo.StartDate.Value == null)
but it always returns false. How come it is not a null? It was not even created..
So.. How to check if DateTime object was not assigned?
Try this: