4

I want to save null value for dateTime field if the DateEdit (dev express C# windows application control) editvalue is null.

If dateEdit Control has a edit value it save the selected date.If its is null it save a default date But i want to save Null value if the dateEdit value is null.

SAmple COde: i am using entity frame work,

Orders ord= new Orders();
ord.OrderDate= dateEdit1.DateTime;**//here i want to save null value if the dateEdit control value is null** 
Objcontext.Orders.AddObject(ord);
Objcontext.SaveChanges();
3
  • Need more information ... how are you writing the values to the table? Post some sample code, please. Commented Sep 7, 2012 at 6:32
  • Thank you for replying me, pls tell me how to save null value to the date field Commented Sep 7, 2012 at 6:40
  • The OrderDate property of the Orders class should be nullable -- it should have the type DateTime? or Nullable<DateTime> (this is two different ways of writing the same thing). I don't know Entity Framework well enough to advise you on how to achieve that. Once you have achieved that, set the property with the null keyword, like this: ord.OrderDate = null; Commented Sep 7, 2012 at 6:52

5 Answers 5

2

are you using ADO Command with Parameters? if so then try something this,

// other codes here
command.AddWithValue("@paramName",  (dateEdit.EditValue == null) ? DBNull.Value : dateEdit.EditValue);

dateEdit Control is a DevExpress component right?

or try

Orders ord = new Orders();
ord.OrderDate = (dateEdit1.DateTime == null ? DBNull.Value : dateEdit1.DateTime); //here i want to save null value if the dateEdit control value is null 
Objcontext.Orders.AddObject(ord);
Objcontext.SaveChanges();

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

You should set the Field property as NULL for accepting NULL values. Otherwise it will save like "0000-00-00".

1 Comment

that field has that property .
0

use Nullable type:

DateTime ? nullableDate = GetSomeDate();

this also applies to other value types.

There is also DBNull class that may be helpful in such operations. Use could be like this:

sqlCommand.Parameters.AddWithValue("created",nullableDate??DBNull.Value);

or similar. starting with .NET Framework 4 MS wants us to pass DBNull.Value for null values.

3 Comments

i am using entity framework pls tell me how to save null value to date field
EF will create nullable types for nullable columns if you do everything right
Thank you aiodintsov Now i saves the null value for the empty dateEdit
0

It may help to store null DateTime for your DateEdit.

DateTime? DateEdit = null;

Comments

0
Orders ord = new Orders();

if(dateEdit1.EditValue!=null)

{ord.OrderDate = dateEdit1.DateTime.Date}

else

{ord.OrderDate= null}

dateEdit control value is null 
Objcontext.Orders.AddObject(ord);
Objcontext.SaveChanges();

Now, I am able to save null value for empty DateTime in the DateEdit.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.