Results 1 to 3 of 3

Thread: [RESOLVED] NoNullAllowedException error handling

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    487

    Resolved [RESOLVED] NoNullAllowedException error handling

    I have a scenario that is raising the System.Data.NoNullAllowedExceptionexception and I don't understand how to intercept (trap) it.

    My form has 2 text boxes and both are bound to columns that don't allow null values (SQLite database fields are configured as "NOT NULL"). I have added the Validating events for these text boxes.

    The following sequence of events raise the NoNullAllowedException exception:

    1. The user opens the form (the first record is shown).
    2. The user clicks the BindingNavigator.AddNewItem button (a new record is shown - the 2 text boxes are empty).
    3. Without entering values in these 2 text boxes, the user clicks the BindingNavigator.MovePreviousItem button.


    I have a global error handler that displays the stack trace.

    Code:
    '...inspect the StackTrace
    If flgDebug Then
        Debug.WriteLine("{0}, {1}", mb.DeclaringType.Name, mb.Name)
        For i = 0 To st.GetFrames.Count - 1
            Debug.WriteLine("{0}. {1}", i, st.GetFrame(i).GetMethod.Name)
        Next i
    End If
    When the error handler is called, the immediate window shows the following sequence of 33 method calls:

    DataColumn, CheckNullable
    0. CheckNullable
    1. RaiseRowChanging
    2. SetNewRecordWorker
    3. InsertRow
    4. FinishAddNew
    5. EndEdit
    6. EndCurrentEdit
    7. ChangeRecordState
    8. set_Position
    9. MoveFirst
    10. OnMoveFirst
    11. RaiseEvent
    12. OnClick
    13. HandleClick
    14. HandleMouseUp
    15. FireEventInteractive
    16. FireEvent
    17. OnMouseUp
    18. WmMouseUp
    19. WndProc
    20. WndProc
    21. WndProc
    22. OnMessage
    23. WndProc
    24. DebuggableCallback
    25. DispatchMessageW
    26. System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop
    27. RunMessageLoopInner
    28. RunMessageLoop
    29. RunDialog
    30. ShowDialog
    31. ShowDialog
    32. tsmiDataWorkingQuestions_Click

    With the exception of method #32 in the stack trace listing above, I don't know how to trap the above stack trace methods so I'm looking for help.

    How can I trap the NoNullAllowedException exception in this scenario? I would like to avoid showing the error and instead call the BindingSource.RemoveCurrent method to abandon the newly added (blank) record.
    Last edited by Mark@SF; Nov 25th, 2021 at 07:38 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: NoNullAllowedException error handling

    I don't often use a BindingNavigator but I'm fairly sure that you can handle the Click events of the Buttons and cancel the navigation, so you could validate the current record and only allow navigation away from it if it passes.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2013
    Location
    San Francisco, CA
    Posts
    487

    Re: NoNullAllowedException error handling

    Thanks, jmc.

    For anyone interested, here's my solution...

    Code:
    '...the Validate() method of the form verifies the value of the control losing focus by causing the Validating and Validated events to occur, in that order. (Inherited from ContainerControl.)
    '...the Validate() method of a ContainerControl class validates the last child control that is not validated and its ancestors up through, but not including, the current container control.
    If Validate() Then
        '...ignore, the Validating() methods for all controls have completed successfully
    Else
        '...in this case, the Validating() method for a control on the form has failed (e.Cancel = True)
        '   and that method will notify the user, for example see the following:
        '    txtAccount.Validating()
        If DirectCast(bsMain.Current, DataRowView).IsNew Then
            '...the user has added a new record but hasn't entered a required value for one or more controls that are bound to fields that cannot be Null
            '   and the DataTable.EndEdit has been implicitly called by one of the BindingNavigator's navigation buttons
            '...the EndEdit method implicitly validates all controls on the form that have a Validating method (see above)
            '   and the associated control that does not have a required value has failed its validation
            MessageBox.Show("The new record's data has not been validated and therefore it will be abandoned.", strMethodName, MessageBoxButtons.OK, MessageBoxIcon.Information)
            bsMain.RemoveCurrent()
        Else
            '...this is not expected
            MessageBox.Show("Unexpected scenario, please contact the application developer.", strMethodName, MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End If
    End If

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width