Setting nullable field to null

Posts   
 
    
Gabbo
User
Posts: 56
Joined: 12-Jun-2006
# Posted on: 08-Aug-2007 21:51:40   

LLBLGen Pro 2.0 Builder 2.0.0.0 Final, March 21st, 2007 Adapter for .NET 2.0 Runtime Library: 2.0.7.319 Database: SQL Server 2005

Hi,

I've used the product for quite awhile and love it, but for some reason this isn't clear to me in the documentation under "Using the entity classes", "Setting a field to NULL".

I'm using the following update pattern:

MyTableEntity entity = new MyTableEntity( primaryKey ); entity.IsNew = false; entity.Field1 = "value"; entity.SetNewFieldValue( (int)MyTableFieldIndex.NullableField2, null ); adapter.Save( entity );

Field1 updates properly, but NullableField2 doesn't update because it's apparently not seen as changed.

I saw a reference to a Fields.IsChanged method, but I don't seem to have that available.

For now I'm just setting it to a value that represents a null, but I'd really like to set it to null. What am I doing wrong? It's a datetime field type if this makes a difference.

Thanks in advance!

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Aug-2007 04:39:51   

Weird disappointed ... Can you confirm that **Nullable checkbox is true **for NullableField2 in LLBLGenPro Designer?

David Elizondo | LLBLGen Support Team
Gabbo
User
Posts: 56
Joined: 12-Jun-2006
# Posted on: 09-Aug-2007 13:33:25   

daelmo wrote:

Weird disappointed ... Can you confirm that **Nullable checkbox is true **for NullableField2 in LLBLGenPro Designer?

Hi, yes, the field in the designer is checked for "(.NET 2.0) Generate as Nullable type", and the field itself in the code is nullable. When set to null the standard way for .NET 2.0 nullable fields:

entity.NullableField2 = null;

it also doesn't get marked as being modified. This is how I originally had it when I noticed it was generating the SQL to update the field to null. I then tried the "old" method of doing it (the example I gave), thinking it might make some difference, but not.

Perhaps there needs to be a different method for marking the field as IsChanged, to force the SQL to generate for cases like this? It's not critical since I can work around it, but it would be nice to have.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 09-Aug-2007 18:21:45   
it also doesn't get marked as being modified. 

Indeed, that's because your field didn't change. You don't fetch Entity, only set PK and IsNew=false, but the other fields remains null. Then you set a field to null, but that field IS ALREADY NULL, then the field don't experiment any change.

Try this:

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

David Elizondo | LLBLGen Support Team
Gabbo
User
Posts: 56
Joined: 12-Jun-2006
# Posted on: 10-Aug-2007 00:24:43   

daelmo wrote:

it also doesn't get marked as being modified. 

Indeed, that's because your field didn't change. You don't fetch Entity, only set PK and IsNew=false, but the other fields remains null. Then you set a field to null, but that field IS ALREADY NULL, then the field don't experiment any change.

Try this:

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

Cool, thanks, I'll give that a try. Maybe this scenario could be added to a future version of the documentation under the 2.0 nullable section?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 10-Aug-2007 05:20:47   

Cool, thanks, I'll give that a try. Maybe this scenario could be added to a future version of the documentation under the 2.0 nullable section?

Reference Manual include such properties and explanations. I will tell Frans about your documentation suggestion wink

David Elizondo | LLBLGen Support Team