Setting a field to NULL

Posts   
 
    
Rosacek
User
Posts: 155
Joined: 18-Mar-2012
# Posted on: 09-Feb-2013 14:35:29   

Hi, I couldn't save null to MSSQL to nullable columns of date by LLBL Pro Runtime v.3.5.12.703, adapter.


Dim lEntityToUpdate As New EntityClasses.OrderBookPoEntity(zaznam.OrderbookPoid) With {
  .IsNew=False 
  .Comment = "something new", _
  .CommentVendor = "ok", _
  .RequestedDate = Nothing}

adapter.SaveEntity(lEntityToUpdate)

Above code updated any field to the new value except RequestedDate Then I found http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=19894 Based on that I moved down the command .IsNew=False right before SaveEntity.

Updated code


Dim lEntityToUpdate As New EntityClasses.OrderBookPoEntity(zaznam.OrderbookPoid) With {
  .Comment = "something new", _
  .CommentVendor = "ok", _
  .RequestedDate = Nothing}

lEntityToUpdate.IsNew = False
adapter.SaveEntity(lEntityToUpdate)

This code successfully updates also RequestedDate to NULL.

Question 1: Why does it depend when IsNew=False is set? I guess it should not matter if IsNew=False is called as first command or last command right before SaveEntity

Question 2: Why does it depend when IsNew=False is set just for fields set to Nothing? Other fields changed to any value are updated correctly, even .IsNew is the first line before any entity field change.

I feel this is strange behaviour of LLBL Pro

In your doc is an example how to "ADAPTER/Using the entity classes, Adapter/Update an entity directly in the persistent storage" And .IsNew is also very first line in your example code, see:


' [VB.NET]
Dim customer As New CustomerEntity()
customer.CustomerID = "CHOPS"
customer.IsNew = False
customer.Phone = "(605)555-4321"
Using adapter As New DataAccessAdapter()
    adapter.SaveEntity(customer)
End Using

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 11-Feb-2013 06:00:16   

Hi Rosacek,

The problem isn't .IsNew. Setting .IsNew to false allows you to emit an Update even where the entity wasn't fetched.

The problem is that when you instantiate the entity, RequestedDate field is NULL, then you set to NULL again, that won't flag the field as 'Changed'. Only 'Changed' fields are included in the UPDATE list.

To workaround this, you can mark the as Changed:

...
// this in C#, you should convert it to VB
lEntityToUpdate.Fields["RequestedDate"].IsChanged = true;
adapter.SaveEntity(lEntityToUpdate);
David Elizondo | LLBLGen Support Team
Rosacek
User
Posts: 155
Joined: 18-Mar-2012
# Posted on: 12-May-2014 21:19:52   

It might be good idea to update example in DOC Option 2: Update an entity directly in the persistent storage http://www.llblgen.com/documentation/4.1/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/Adapter/gencode_usingentityclasses_adapter.htm#Modifyinganentity

and move down the statement customer.IsNew = False to be the last one right before Using adapter ... With some note, that entity.IsNew = False should be the last entity setting, because if you want to update some fields to NULL, then IsNew=False should be bellow that. Therefor the best position of .IsNew=False is the last line before save.

' [VB.NET]
Dim customer As New CustomerEntity()
customer.CustomerID = "CHOPS"
customer.Phone = "(605)555-4321"
customer.Fax=Nothing
customer.IsNew = False
Using adapter As New DataAccessAdapter()
    adapter.SaveEntity(customer)
End Using
daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 13-May-2014 07:49:36   

Also setting .IsChanged to true does the trick. Manipulating .IsNew is not the official way of doing things, so we don't promote that workaround, but still, it's usable sometimes. We will look forward into your documentation observation. Thanks for the feedback.

David Elizondo | LLBLGen Support Team
Otis avatar
Otis
LLBLGen Pro Team
Posts: 39590
Joined: 17-Aug-2003
# Posted on: 13-May-2014 10:35:57   

Updated. simple_smile

Frans Bouma | Lead developer LLBLGen Pro