StrataFrame Forum

Delete on a Maintenance Form

http://forum.strataframe.net/Topic30170.aspx

By Ger Cannoll - 7/20/2011

When I delete a row from within a Maintanance Form, I notice it deletes from the Database immediaitely, unlike when I edit rows, I can Undo before I save.

Is there a way of 'Holding' the deletes until I click on Save, so that any Deletes/ Edits are all done at the same time , an I am also given an option to 'Undo', for deletes
By Ivan George Borges - 7/20/2011

Hi Gerard.

Have a look at the following:

  • DeleteCurrentRow() - There are three overloads to this method. Depending on the parameters passed, this method will behave differently.
      • DeleteCurrentRow() - When this overload is called, the record being deleted is immediately removed from the server and the internal data table is updated.

      • DeleteCurrentRow(OnlyMarkAsDeleted) - When OnlyMarkAsDeleted is given a value of True, the current row is deleted within the internal data table only and a Save() must be executed before the changes are reflected back to the server. This is recommended when deleting a large number of records.

      • DeleteCurrentRow(CheckSecurity, OnlyMarkAsDeleted) - When a value of True is passed to the CheckSecurity parameter, a security check will be forced by raising the CheckSecurity event on the business object, allowing the developer to place any Security on Delete code. The OnlyMarkAsDeleted parameter behaves identically to the DeleteCurrentRow(OnlyMarkAsDeleted) overload described above.

    Note: All deletion methods return the number of records affected by the deletion. If a zero is returned, no records were deleted from the server.

    By Ger Cannoll - 7/20/2011

    Hi Ivan. Tnaks for replying.

    How do I access the DeleteCurrentRow method with the various over-rides from a SF Maintanace Form ?
    By Edhy Rijo - 7/20/2011

    Hi Gerard,

    Gerard O Carroll (7/20/2011)
    How do I access the DeleteCurrentRow method with the various over-rides from a SF Maintanace Form ?


    You can handle the BO.BeforeDelete event and manipulate the delete routine as you wish and then set the e.Cancel to True as shown in the code below:

        Private Sub BizVendor1_BeforeDelete(e As MicroFour.StrataFrame.Business.BeforeDeleteEventArgsHandles BizVendor1.BeforeDelete
             e.BusinessObject.DeleteCurrentRow(True)
             e.Cancel = True
         End Sub
    By Ger Cannoll - 7/20/2011

    Hi Edhy.

    I put following code into the BeforeDeleteEvent:

    e.BusinessObject.DeleteCurrentRow(true, true);
    e.Cancel = true;


    It goes round in an infinite loop with following question: Are you sure you wish to delete the current record ? where I can select a yes or No button. My table only has about 10 records .

    This is on a Standard maintenance form and I dont have any other code in there.

    By Edhy Rijo - 7/20/2011

    Hi Gerard,

    Why are you using the 2 overloads?  are you checking security for deleting the record? if not, just use one parameter of True to mark the record as deleted without saving it to the database.
    By Ger Cannoll - 7/20/2011

    Hi Edhy.

    I was using the two overloads to 'Future Proof' tha app (I dont have Security switched on right now but do intend to )

    I chnged just to have one overload and same thing... just keeps looping asking If I am sure I want to delete
    By Ivan George Borges - 7/20/2011

    Gerard, you are getting an infinite loop because you are calling a Delete inside the BeforeDelete, which will trigger another BeforeDelete and so on.

    You can either create your own Delete button and hide the one from the toolbar, or still use the BeforeDelete with the help of a flag. Something like:

    #Region "  Private Fields  "

        Private _WeAreDeleting As Boolean = False

    #End Region

    #Region "  Handled Events  "

        Private Sub MyBO_BeforeDelete(ByVal e As MicroFour.StrataFrame.Business.BeforeDeleteEventArgs) Handles MyBO.BeforeDelete
            '-- if we are currently deleting, exit
            If _WeAreDeleting = True Then
                Exit Sub
            End If

            '-- set flag so we won't get into a looping
            _WeAreDeleting = True

            '-- mark for deletion
            MyBO.DeleteCurrentRow(True)

            '-- cancel standard deleting procedure
            e.Cancel = True

            '-- reset flag, since we are done
            _WeAreDeleting = False
        End Sub

    #End Region
    By Edhy Rijo - 7/20/2011

    Hi Ivan,

    Thanks for the clarification and the flag idea, it makes sense.

    Ivan George Borges (7/20/2011)
    Gerard, you are getting an infinite loop because you are calling a Delete inside the BeforeDelete, which will trigger another BeforeDelete and so on.

    That looks like a minor bug somewhere since the e.Cancel = True should be stopping any delete process, right?
    By Ivan George Borges - 7/20/2011

    Hey Edhy!

    Not really a bug. Remember we are starting a brand new Delete command right inside the BeforeDelete. This brand new Delete will call its own BeforeDelete, which is... this same BeforeDelete where we are. And then it will again hit the Delete call, which will trigger another one, and another one, and...
    By Edhy Rijo - 7/20/2011

    Hi Ivan,
    I will check out the source when I have some time because I still believe that canceling the before event should stop the delete from continue.
    Ex:
    1. Click Delete Button in Toolbar
      • BeforeDelete method is called in the Form.
        • BeforeDelete method in the BO is raised.  At this point setting e.Cancel = True should stop the chain reaction, so I don't understand why the BeforeDelete is being called again and again and again?

    Since I use the StrataFlix logic in all my applications, I have not seen this problem because this logic uses a custom Delete method and it works just fine as expected.
    By Ivan George Borges - 7/20/2011

    But Edhy, the original Delete is being Canceled just fine. What is triggering the other BeforeDelete is de DeleteRow that was coded inside it. This one will also trigger a BeforeDelete.
    By Ger Cannoll - 7/21/2011

    Hi Ivan. Thanks for the code, and it has got over the infinite loop. However, it asks me TWICE now if I want to delete. My Code is:

    // Set up a variable to prevent an infinite loop as we are calling DeleteCurrentRow from inside DeleteCurrentRow event
    private bool _CurrentlyDeleting = false;
    private void scpBO1_BeforeDelete(MicroFour.StrataFrame.Business.BeforeDeleteEventArgs e)
    {
    if (_CurrentlyDeleting == true)
    { return; }

    _CurrentlyDeleting = true;
    e.BusinessObject.DeleteCurrentRow(true);
    e.Cancel = true;
    // Reset flag once done
    _CurrentlyDeleting = false;
    }


    Any ideas on why it is asking twice
    By Ivan George Borges - 7/21/2011

    Good to know, glad we moved on.

    I am out of the office right now and I have to be quick, but basically you need to switch the form's automatic delete question (set the AutoShowDeleteConfirmation to False) and roll your own and use it inside the BeforeDelete, checking if the user confirmed the action before you mark the row for deletion.
    By Ger Cannoll - 7/21/2011

    Hi Ivan.

    I set the switch and inserted my own code , which works fien. Many thanks.

    I am curious to know whey the the SF message was coming up twice though. As a general priciple, my preference is to let the fremawork do Most Stuff, and avoid any 'Custom Coding' where possible. As I will proabbaly use this Delete Functionality in a good number of forms, Ids like to understand why the message is coming up twice, and avoid it if possible without additional coding.
    By Ivan George Borges - 7/21/2011

    It was coming twice because you are issuing delete twice. The message will come up before the BeforeDelete event, so it came for the Delete you clicked on the toolbar and then afterwards for the Delete you coded inside the BeforeDelete.

    Glad you got it working!