Changing Primary Key - Insert failing, expecting Update

Posts   
 
    
JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 25-Jul-2007 12:12:32   

I have a table with four fields making up the primary key. I need to be able to change one of these values but I still need it to be part of the primary key. When I try to modify that value and a few others, it fails, saying that a different, non null element can't be null and, crucially, that the insert failed, where I was expecting an update

Dim Refund As New BookingDB.EntityClasses.TicketRefundEntity(RefundsList.Rows(i)("SheetRef"), _
    RefundsList.Rows(i)("BookingRef"), RefundsList.Rows(i)("TicketNumber"), RefundsList.Rows(i)("MachineNumber"))

Refund.Save() ' Debug - This save works

Refund.FullRefundReason = RefundsList.Rows(i)("FullRefundReason")
If cmbView.Text = "Un-Printed" Then
    Refund.JourneyCharge = RefundsList.Rows(i)("JourneyCharge")
    Refund.GrossRefund = RefundsList.Rows(i)("GrossRefund")
    Refund.Atocfee = RefundsList.Rows(i)("Atocfee")
    Refund.AgencyFee = RefundsList.Rows(i)("AgencyFee")
End If

If ChangePeriod Then
    Refund.Period = RefundsList.Rows(i)("Period")
    ' This is where I need to change the Sheet Reference. I need to keep
    ' this in the primary key so tickets can be resubmitted if rejected.
    If cmbSheet.Text = "New" Then
        Refund.SheetRef = RefundsList.Rows(0)("TicketNumber") & RefundsList.Rows(0)("MachineNumber")
    Else
        Refund.SheetRef = cmbSheet.Text
    End If
    SheetRef = Refund.SheetRef
End If
Refund.Save()

I am using Self Servicing with VS2005

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Jul-2007 16:07:02   

When change the value of the field, does the PK Fields values correspond to a row in the database?

Make sure the entity.IsNew property is set to false, before the last save.

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 25-Jul-2007 16:32:01   

I added Refund.IsNew = False and now it fails with the error: "During a save action an entity's update action failed. The entity which failed is enclosed."

Any ideas on how to investigate this further? It is currently in a try loop. Would removing that help me to get further info?

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Jul-2007 16:43:47   

It's better to examine the generated SQL Query if any. And then try to execute it direcly against the database. To get the generated query, please check the following manual's section: Using the generated code -> Troubleshooting and debugging"

JMitchell avatar
JMitchell
User
Posts: 128
Joined: 01-Sep-2006
# Posted on: 25-Jul-2007 17:28:48   

Thanks Walaa. It turned out I had the primary keys in the wrong order when I was creating the instance of the entity. All sorted now.