Hey guys,
My app is blowing right through the business rule evaluation (on a deletion) and not halting at a broken business rule and I am not quite sure why or what part I'm missing. Direct calls to the bo.RejectChanges() method don't stop the deletion and I can not find a way to halt the deletion and return to the calling form.
I have two tables, [car] and [car_type], where [car_type] is the "Parent" or domain table, fk'd with car.car_type_id Referencing car_type.car_type_id.
I have an interface to interact with both tables and I am testing the error condition, namely trying to delete a 'type' to which 'cars' are associated.
What I am trying to do is prevent a user from deleting a car_type if there are cars associated to it.
boCars has the following method - it counts the number of cars associated with a car_type
Public Function GetCountByType(ByVal intCarTypeID As Integer) As Integer
Dim loCommand As New SqlCommand()
loCommand.Parameters.Add("@_car_type_id", SqlDbType.Int)
loCommand.Parameters("@_car_type_id").Value = intCarTypeID
loCommand.CommandText = "SELECT COUNT(*) FROM car " & _
"WHERE car_type_id = @_car_type_id"
Return CType(Me.ExecuteScalar(loCommand), Integer)
End Function
And boCarType has following sub on the BeforeDelete eventhandler - it uses boCar.GetCountByType to count the related records and if it finds any it is supposed to add a broken rule, with the intention of stopping the execution of the deletion.
Private Sub boCarTypes_BeforeDelete(ByVal e As MicroFour.StrataFrame.Business.BeforeDeleteEventArgs) Handles Me.BeforeDelete
Dim intGoNoGo As Integer
Dim boCar As New boCars
Dim strErr As String
Try
intGoNoGo = boCar.GetCountByType(Me.car_type_id)
If (intGoNoGo > 0) Then
strErr = "There are cars associated with this " & _
"'car type'. In order to remove a 'car type' all " & _
"of the cars associated with it must reassigned " & _
"or deleted using the 'cars' form."
Me.AddBrokenRule(boCarTypesFieldNames.car_type_name, strErr)
'Me.RejectChanges()
End If
Catch ex As Exception
_m_Log.LogMe("boCarTypes_BeforeDelete" & ": " & ex.Message)
Finally
boCar.Dispose()
intGoNoGo = Nothing
strErr = Nothing
End Try
End Sub
Now, when I trace through the code (the delete event invoked from the toolstrip delete button) everything seems to work fine (code above is executed, boCar.GetCountByPK returns > 0, "Me.AddBrokenRule" executes(Me.RejectChanges seems to have no effect)) except that the code just merrily runs along and the bo tries to commit the deletion despite the broken rule. Of course this results in an unhandled exception (which I could handle and log but I am trying to prevent it in the first place) due to the fk violation - there are child records associated with the parent record I am trying to delete.
Note that the "ParentBusinessObject" property is not set on boCar as it could have multiple parents and I am handling all of the relationships manually, this just happens to be a simple 1-1 (object to object) relationship.
So what it comes down to is
1.) There is an intentional error - an fk violation - that I am testing
2.) All the code and events appear to be firing correctly
3.) The code for adding a broken rule is being executed
4.) Despite there being a broken rule, code execution continues and it tries to commit the delete resulting in the error
To wrap it all up - I can detect the error condition beforehand - how do I stop the execution of the business object's deletion routine? (Similar to a rollback but before the db call is even made.)
There has gotta be a way - either I'm just missing it or I'm not seeing my own error somewhere.
Thanks for your time - J