Handling Null Values RRS feed

  • Question

  • User1122355199 posted

    Hello everyone and thanks for your help in advance.  I have inherited some code that I now need to debug.  At the heart of the problem is the handling of null values within a class.  Here is a code sample:

    Private mstrModifier1 As String
        Public Property Modifier1() As String
                Return mstrModifier1
            End Get
            Set(ByVal value As String)
                mstrModifier1 = value
            End Set
        End Property

    The value is set by parsing a text string:

            If Len(CurrentLineItem.CPTCode) > 10 Then
                'Get the Modifier 1
                CurrentLineItem.Modifier1 = Mid(CurrentLineItem.CPTCode, 10, 2)
            End If

    In other words, the current line may not always set to a value.  Since the variable is not initialized to a value when declared, I think the variable may be set to null.   In these cases, when I try to write to a database using:

    sqlComm.Parameters.AddWithValue("mod1", CurrentLineItem.Modifier1)

    I receive an error Procedure or function 'sproc' expects parameter '@mod1', which was not supplied.  The column is set up accept null values.  So I'm not exactly sure of the best way to fix this problem.  I know I can set the value of the variable of the string to "" at the declaration.  Or I could check to see if the value is set and if not, set it to null or set up the parameter as nullable.  I guess I'm looking at guidance on what the best practice would be.  Any help would be appreciated.

    Thursday, October 22, 2015 7:08 PM


  • User-1716253493 posted
    sqlComm.Parameters.AddWithValue("mod1", IIF(CurrentLineItem.Modifier1=nothing, dbnull.value,CurrentLineItem.Modifier1))

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 22, 2015 9:43 PM