I have a nullable DateTime Variable. And I want to write it to SQL DB. When i try to insert:
If the variable has value there is no problem.
But if it hasn't a value, insertion interrupting with an error.
I want to ask: How can we insert nullable DateTime to Sql via DbCommand Parameter?
(P.S. : Sql column is nullable too.)
DateTime? myDate = null;
DbCommand dbCommand = new DbCommand();
dbCommand.Parameters.Add("NullableSqlDateField", DbType.DateTime, myDate);
Try the null coalescing operator:
dbCommand.Parameters.Add("NullableSqlDateField", DbType.DateTime, (object) myDate ?? DbNull.Value);
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