Handling Delete When a Parent/Child Relationship Exists


Author
Message
fparker
fparker
StrataFrame Beginner (49 reputation)StrataFrame Beginner (49 reputation)StrataFrame Beginner (49 reputation)StrataFrame Beginner (49 reputation)StrataFrame Beginner (49 reputation)StrataFrame Beginner (49 reputation)StrataFrame Beginner (49 reputation)StrataFrame Beginner (49 reputation)StrataFrame Beginner (49 reputation)
Group: Forum Members
Posts: 31, Visits: 199
I have created a maintenance form with two business objects on it in a parent/child relationship.  I set the form's IncludeInFormDeleteType property to AllBusinessObjects.  When I click the Delete button on the MaintenanceFormToolStrip, I get two messages asking me to confirm the delete operation and the only child record that gets deleted is the one that is considered current.  How can I get the delete button to delete the parent record and all of its children with only one confirmation message?  Thanks for your help.

Fran Parker

MaxQ Technologies, Inc.

Trent Taylor
Trent Taylor
StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)StrataFrame Developer (14K reputation)
Group: StrataFrame Developers
Posts: 6.6K, Visits: 7K
First of all, the IncludeInFormDelete is only be concerned with the current records.  So I would set this back to the PrimaryBusinessObject.  Next, I would handle the deletion of the child in the AfterDelete event of the primary business object.  Generally what I do is get a list of all of the children PKs that I want to delete, the cycle through and delete them all.  You could also create a command in the child BO that uses parameters to delete all of the children:

Public Function DeleteChildrenRecords(Byval ParentPrimaryKey As Integer) As Integer
    Dim loCommand As New SQLCommand

    '-- Create the command
    loCommand.CommandText = "DELETE FROM MyTable WHERE MyField_ParentPK = @MyField_ParentPK"

    '-- Create the parms
    loCommand.Parameters.Add("@MyField_ParentPK", SqlDbType.Int)

    '-- Set the parms
    loCommand.Parameters("@MyField_ParentPK").Value = ParentPrimaryKey

   '-- Return the number of records deleted
   Return Me.ExecuteNonQuery(loCommand)
End Function

So in the BeforeDelete or AfterDelete event of the BO on the form, call the method above:

MyChildBO.DeleteChildrenRecords(Me.MyPrimaryKey)

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search