How to set Sql DateTime to null from LINQ

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

enter image description here

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, -, .

:)

+5
2

:

Customer customer = new Customer();
customer.CustomerName = txt_name.Text;
customer.DOB = dtp_dob.Checked 
                  ? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd")) 
                  : (DateTime?) null;

null DateTime?. , .

, , nullTime, 1 0001 . :

DateTime? nullDateTime = null;
customer.DOB = dtp_dob.Checked 
                  ? DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd")) 
                  : nullDateTime;
+13

PLZ

if(dtp_dob.Checked )
{
   customer.DOB = DateTime.Parse(dtp_dob.Value.ToString("yyyy-MM-dd"));
}
else
{
   cstomer.DOB =null;
}

DOB , , . BTW nullDateTime.Value

0

All Articles