none
How do I get the column name that caused a NoNullAllowedException?

    Question

  • How do I get the column name that caused a NoNullAllowedException?

    The column name is in the exception message (Column 'SCID' does not allow nulls.), but I would like to get just the column name that caused the error. Thank you. -ME

     Private Sub IDetailBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs) Handles IDetailBindingNavigatorSaveItem.Click
            Try
                Me.Validate()
                Me.IDetailBindingSource.EndEdit()
                IDetailTableAdapter.Update(SSTDataSet.iDetail)
                Me.InitiativeSupplementTableAdapter.Fill(Me.SSTDataSet.initiativeSupplement)
    
            Catch dbcx As Data.DBConcurrencyException
                Debug.WriteLine(dbcx.Message)
    
            Catch dbNoNullx As Data.NoNullAllowedException
                Debug.WriteLine(dbNoNullx.Message)
    
            Catch ex As Exception
                Debug.WriteLine(ex.Message)
            End Try
    
        End Sub

    Saturday, March 11, 2017 4:24 PM

Answers

  • Investigate the Data member of dbNoNullx. Maybe there is a key that contains the column name. Otherwise you can extract the column from the message. An example:

    Dim message = "Column 'SCID' does not allow nulls"

    Dim column = Regex.Match(message, "Column '(.*)' does not allow nulls").Groups(1).Value


    • Edited by Viorel_MVP Saturday, March 11, 2017 8:12 PM
    • Marked as answer by kingelk Sunday, March 12, 2017 10:10 AM
    Saturday, March 11, 2017 8:10 PM