I have two cases where I need to set the DateTime field in Sql to null. I am using C # and LINQ to SQL classes. I read a lot of stackoverflow questions that are similar to my question, but still I am fueling me a bit.
When we insert a new customer.
Relevant Code:
Customer customer = new Customer();
customer.CustomerName = txt_name.Text;
customer.DOB = dtp_dob.Checked ? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd")) : //send null here;
customer.DOB - System.DateTime.
What I tried:
1)
customer.DOB = dtp_dob.Checked ? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd")) : SqlDateTime.Null;
But this will not be done, since SqlDateTime cannot be converted to System.DateTime.
2)
DateTime? nullDateTime = new DateTime();
customer.DOB = dtp_dob.Checked ? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd")) : nullDateTime.Value
In this case, the assembly succeeds, but it throws a SqlDateTime overflow exception.
So how to pass null
LIN Dataclass DOB Member Property

Many of them suggest setting the Auto Generated Value to true and not pasting any value. True, it works in cases of insertion.
, , datetime DOB. ( ), UpdateOn, -, .
:)