Insert and Update

Posts   
 
    
baiju
User
Posts: 3
Joined: 21-Apr-2006
# Posted on: 22-Sep-2006 22:47:04   

Hi,

I am developing an application where I need to save an entity. Before I save, I need to check whether object is to be Updated or Inserted. Currently, I am using this method and it works.

Dim objEmpHistory As New EntityClasses.ApemploymentHistoryEntity If objEmpHistory.FetchUsingPK(MyBase.Participant.ParticipantId, ItemNumber) = False Then objEmpHistory.ParticipantId = MyBase.Participant.ParticipantId objEmpHistory.ItemNumber = ItemNumber End If objEmpHistory.JobTitle = txtCurrentJobTitle.Text objEmpHistory.Save()

ParticipantId and ItemNumber are key fields.

Is there a better way to check whether the record is present in database than this? Over here, when I do FetchUsingPK(), the returned object has the Primary key fields set, even if it is not present in database, so when I save, it gives an error unless I set them again with same value.

Thanks in advance.

Baiju Nagori

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 23-Sep-2006 11:18:42   

You could execute a getscalar query for the PK. You can also check the IsNew property of the entity. If it's true, it will be inserted, otherwise updated.

Frans Bouma | Lead developer LLBLGen Pro