.Save problem

Posts   
 
    
miman
User
Posts: 2
Joined: 17-Feb-2005
# Posted on: 17-Feb-2005 15:02:38   

Hi,

I'm having a problem with a table which has a two fields as the primary key. The key is a task_id and task_sub_id. When I try to create more than one record, it creates the first record correctly but when I try to insert the second it updates the first record rather than creating a new one. I've debugged it and even though the sub_id is changed, it still updates instead of inserting.


Dim newtask As New ORDER_TASKSEntity
If e.CommandName = "SelectTask" Then
     e.Item.BackColor = System.Drawing.Color.Cyan
    newtask.ORDERNR = OrderNr.Text
    newtask.ORDER_GROUP = txtOrderTypesddl.SelectedValue()
    newtask.ORDER_SEQ = 1
    newtask.TASK_STATUS = "UNA"
    newtask.TASK_PRODUCTCODE = e.Item.Cells(3).Text
    newtask.ORDER_TYPE = e.Item.Cells(2).Text
    newtask.TASK_CIRCUIT = e.Item.Cells(1).Text
    newtask.TASK_TYPE = e.Item.Cells(4).Text
    newtask.DATE_ASSIGNED = Date.Now
    Dim i As Integer
    PopDDL = CType(e.Item.FindControl("PopDDL"), ListBox)
    ActionProcedures.GET_NEXT_TASK_ID(task_id)
    For i = 0 To PopDDL.Items.Count - 1
        If PopDDL.Items.Item(i).Selected Then
            newtask.TASK_ID = task_id
            newtask.TASK_SUB_ID = i + 1
            newtask.TASK_POP_LOCATION = PopDDL.Items.Item(i).Value
            Try
                newtask.Save()
                newtask.TransactionCommit()
            Catch ex As Exception
                Dim mess As String
                mess = ex.ToString
            End Try
        End If
    next

Any ideas?

Thanks

Mike

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Feb-2005 15:46:56   

You're always saving the same entity object, newTask. So the next loop iteration, you're changing the just saved newTask entity and save it again. This is caused by the fact that after the newTask.Save() call, newTask isn't new anymore.

So after newTask.Save() do: For Each field As EntityField in newTask.Fields field.IsChanged=True Next newTask.IsNew = True

then, next loop iteration, the entity is brand new and will be saved again as a new entity. Though, if you need this, it's perhaps a good idea to normalize your database a bit.

Also, remove the transaction.Commit() from the for loop. You should commit the transaction AFTER the forloop has been completed, not during the loop. And make sure that if an exception is caught, the forloop is terminated and the exception handler rollsback the transaction, you're not doing that now.

Frans Bouma | Lead developer LLBLGen Pro
miman
User
Posts: 2
Joined: 17-Feb-2005
# Posted on: 17-Feb-2005 16:30:59   

Thanks for the help, but the solution required the newtask to be a collection not an entity. But knowing that it was not classed as new when the save() was done meant that all I had to do was define the entity as new within the for loop. It now works. It may not be elegant but it works.

As for further normalisation, it's not possible at present. We have to make do with what we've got.

Thanks for putting me on the right track

Mike