I try to insert the value null (DateTime) in my database for a field typed 'date' but I always get a '0001-01-01'. I don't understand, this field "allow nulls" and I don't know why I have this default value.
I'm using C# asp .net with MVC (Entity Framework), this is my code :
Budget_Synthesis newBS = new Budget_Synthesis
{
Budget_Code = newBudgetCode,
Last_Modified_Date = null
};
db.Budget_Synthesis.AddObject(newBS);
Last_Modified_Date is typed System.DateTime? so I don't know why they change this 'null'.
If I try to display the value on my application I get 01/01/0001 00:00:00
And 0001-01-01
with SSMS
Someone can explain me why I can't get a real 'NULL' ?
Best regards
I think this is the value corresponding to the null
If
Last_Modified_Date
is of typeDateTime
, you can't have "real null" because DateTime structure - as others already said- is not nullable. So your sample code will not even compile.If
Last_Modified_Date
is of typeDateTime?
(Nullable<DateTime>
) your code is correct, but -as @Nikola Dimitroff said in his answer- you can't have "real null" in your database because the default value forDateTime?
is 01/01/0001 00:00:00.The "real null" you are looking for is
DBNull.Value
, but you can use it only for System.DBNull type; if you assignLast_Modified_Date = DBNull.Value
, whatever the type ofLast_Modified_Date
is, your code will not compile.When saying you are trying to put a null
DateTime
, are you using aNullable<DateTime>
(a.k.aDateTime?
) or simplyDateTime
? The latter is a value type and its default value is precisely01/01/0001 00:00:00