none
Catching an InvalidConstraintException from the BindingNavigator Delete button RRS feed

  • Question

  • Using VisualBasic2008, I have a DataSet with related tables which have a ForignKeyConstraint DeleteRule set to None. A BindingNavigator is used to manipulate the records in a Parent table that holds the PrimaryKey used in the subject relationship. I want to catch the InvalidConstraintException that is thrown when the user clicks the Delete button on the BindingNavigator so I can issue a warning and solicit the desired action from options offered. I need help figuring out where to code the Try … Catch block as the click action on the BindingNavigator Delete button executes system level code (looking at the Call Stack once halted with the error in the debugger shows only [External Code]).. I tried placing a Try … Catch in the BindingNavigatorDeleteItem_Click event procedure with a null Try section but the Catch ex As System.Data.InvalidConstraintException is not executed and the code is stopped in the debugger with the system generated InvalidConstraintException message.

    Where do I put the Try … Catch to make this work?

    Eric

    Friday, March 5, 2010 4:38 PM

Answers

  • You are encountering a common frustration: the BindingNavigator is not very flexible.  The key to improving things is to recognize that it is basically a simple ToolStrip with some buttons that call BindingSource methods such as MovePrevious, MoveNext, and (of particular interest to you) RemoveCurrent.

    You can remove the delete button from the BindingNavigator and add another button in the visual designer.

        'Click code for new ToolStripItem that you add to the BindingNavigator.
        Private Sub DeleteToolStripButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteToolStripButton.Click
            Try
                BindingSource1.RemoveCurrent()
            Catch ex As Exception
                'Your code here.
            End Try
        End Sub




    • Marked as answer by Eric-67220 Saturday, March 6, 2010 4:48 PM
    Saturday, March 6, 2010 2:50 PM