Difficulty with adding broken rules


Author
Message
StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Oh, and all of the "Before" events have a Cancel in their event args.  So, if you need to abort an Undo(), or abort a Save(), or abort an Add() (BeforeAddNew and AfterAddNew are being added to the class right now), then you can set Cancel = True and it will abort the action; then the corresponding "After" event won't be raised either.
StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Glad you got it sorted Smile

If you need anything else, we're here...

JKelly
JKelly
StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)
Group: Forum Members
Posts: 19, Visits: 149
Thanks for the quick reply, Ben. Looks like we were both writing the answer at the same time.

And thank you for the additional info, too.

- J

JKelly
JKelly
StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)
Group: Forum Members
Posts: 19, Visits: 149

Okay - I was missing something. I just found the following...

' [Given the bo declaration ... ]

Private Sub boCarTypes_BeforeDelete(ByVal e As MicroFour.StrataFrame.Business.BeforeDeleteEventArgs) Handles Me.BeforeDelete

...

' [Take 'e' as the "MicroFour.StrataFrame.Business.BeforeDeleteEventArgs" member and on error pop a modal, instructional dialog and set cancel to true]

e.Cancel = True

Sorry for the noise. I knew this kind of thing had to exist I just couldn't find it.

Tx - J  


StrataFrame Team
S
StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)StrataFrame Developer (6.5K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
The approach you're taking is one that can be taken within the CheckRuleOnCurrentRow() event, but not the BeforeDelete event.  You can add broken rules all day long within the CheckRuleOnCurrentRow event and it won't stop the delete because a delete doesn't check broken rules.  If you want to abort the delete, then you need to do this:

e.Cancel = True

Setting the Cancel property of the BeforeDeleteEventArgs to True will cause the delete to be aborted (the AfterDelete event won't even be raised in that case).  Also, the reason calling RejectChanged doesn't work is because the DataRow.Delete hasn't been called, yet.  If you call RejectChanges within the AfterDelete event, then it would undo it because DataRow.Delete() gets called between the BeforeDelete and AfterDelete events. 

Since you cannot add broken rules within the BeforeDelete event handler, if you want to show an error on a specific form, you can get to the ErrorProvider on the form with Form.SetErrorProvider() and Form.ClearErrorProvider, which will allow you to show errors on fields manually (using the same error provider settings, like the icon, that you set on the form). 

Hope this helps Smile

JKelly
JKelly
StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)StrataFrame Beginner (27 reputation)
Group: Forum Members
Posts: 19, Visits: 149

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


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