Cannot save a DateTime nullable field

Posts   
 
    
rracer99
User
Posts: 58
Joined: 11-Mar-2007
# Posted on: 04-Dec-2007 08:37:30   

Hello,

I am trying to save an entity with a DateTime nullable field (allow nulls is true in the DB). When I turn on trace I see that the nullable field is not included in the UPDATE statement.

How can I save a null value in this case? I had been trying entityName.Field = null. No exceptions are thrown, it just is not included in the UPDATE to the DB.

Using adapter with MS SQL 2005 with LLBLGEN 2.5 Nov 5th build.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Dec-2007 08:48:32   

What was the value of this field before you set it to null?

rracer99
User
Posts: 58
Joined: 11-Mar-2007
# Posted on: 04-Dec-2007 09:05:24   

Walaa wrote:

What was the value of this field before you set it to null?

The pre-existing value is valid DateTime (i.e. 2007-12-01 00:00:00).

The entity will save the correct DateTime if I save a DateTime value, it is only the null value that is being ignored.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Dec-2007 09:20:20   

As this information was missing, I assume you are using .NET 2.0, right? Was this field being generated as Nullable .NET Type? Otherwise you can use the folloiwng code:

myEntity.SetNewFieldValue((int)MyEntityFieldIndex.MyDateField, null);
rracer99
User
Posts: 58
Joined: 11-Mar-2007
# Posted on: 04-Dec-2007 09:44:15   

Yes it is .Net 2.0 (2.5 technically).

1) Unfortunately your suggestion does not work. 2) When attempting using a nullabe date time, I try:

DateTime? nullDate = null; myEntity.MyField = nullDate;

And same problem.

Field is not included in UPDATE statement in both cases.

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Dec-2007 10:35:39   

Yes it is .Net 2.0 (2.5 technically).

There is no .NET 2.5, either 1.0, 1.1, 2.0, 3.0 or 3.5 I think you have mistaken it with the LLBLGen Pro version.

1) Unfortunately your suggestion does not work.

Did you mean the following line didn't work?

myEntity.SetNewFieldValue((int)MyEntityFieldIndex.MyDateField, null);

2) When attempting using a nullabe date time, I try:

DateTime? nullDate = null; myEntity.MyField = nullDate;

And same problem.

So myEntity.MyField is of type "DateTime?", correct?

rracer99
User
Posts: 58
Joined: 11-Mar-2007
# Posted on: 04-Dec-2007 11:07:06   

Oops, I mean't .Net v2.0.50727

Yes I tried:

myEntity.SetNewFieldValue((int)MyEntityFieldIndex.MyDateField, null);

Yes,

myEntity.MyField is type DateTime? (and allow nulls is set for this column in the DB)

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Dec-2007 11:12:32   

strange.

Would you please attach a simple repro solution based on Northwind or on a schema of your own, but then you should also provide the appropriate script to create such schema?

rracer99
User
Posts: 58
Joined: 11-Mar-2007
# Posted on: 04-Dec-2007 11:22:41   

I was hoping not to have to do that

Can you first attempt to save a null DateTime value into MS SQL 2005 to see if you can re-produce this?

Are there any designer options I am missing that could be causing this behavior? Again, any valid DateTime value will save properly.

And yes the designer sees this as a nullable column. The type in .Net is DateTime?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 04-Dec-2007 11:27:11   

Did you fetch the entity before updating it? Would you at least post the corresponding code snippet, where you fetch/create the entity, modify the dateTime value and attemt to save it?

(EDIT) Might be the same issue as in here: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=10817

rracer99
User
Posts: 58
Joined: 11-Mar-2007
# Posted on: 04-Dec-2007 12:10:13   

Thats IT! Fetching the entity first will fix this problem.

Is there a way to set a single field to a null value without fetching first (making a DB trip) ?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 05-Dec-2007 11:30:47   

Is there a way to set a single field to a null value without fetching first (making a DB trip) ?

Yes as shown in the previously linked thread, Set the IsChanged property to true:

youEntity.Fields["YourFieldName"].IsChanged = true;