Reputation: 26517
I have a web form (.aspx) that has a textbox. My user can enter a value into the textbox as a persian date. what should I do is converting texbox.text into DateTime. But sometimes,the textbox has no text within it and so I should take care of it and return null.
when I return null how can I insert null (c#) into DB?
Upvotes: 1
Views: 9026
Reputation: 1062770
It all depend on how you are doing your data access.. by hand?
If so; there are two common ways of passing a null DateTime
around; the original was to use DateTime.MinValue
to mean null
; the second is to use Nullable<DateTime>
(aka DateTime?
) to pass the date around. Then in your DAL you detect this and switch to DBNull.Value
:
if(someDate == DateTime.MinValue) { // or "== null" for Nullable<DateTime>
dateParam.Value = DBNull.Value;
} else {
dateParm.Value = someDate;
}
With LINQ-to-SQL, the Nullable<DateTime>
approach should "just work".
Upvotes: 4