Storing null in database column. How do I do that? RRS feed

  • Question

  • User1080700311 posted

          I need to figure out a way to store a null value into a column in a database table. I tried assigning DBNull.Value()
          but the compiler does not like DBNull.Value(). How can I do this in VB.net ? A C# developer on this forum offered the
          following one line C# solution, but I can't figure out somthing similar for VB.net. I used the web conversion tool
          from C# to vb.net but the conversion was wrong.

          C# solution: sprocComm.Parameters["@dateOfExpire"].Value = String.IsNullOrEmpty(dateToSave) ? DBNull.Value : dateToSave;


          My VB.net solution which won't work
            If txtExpireDate.Text <> "" Then
                Dim delimiterChars As Char() = {" ", ",", ControlChars.Tab}
                Dim theDate As String() = txtExpireDate.Text.Split(delimiterChars)
                dateToSave = ConvertDateToYYMMDD(theDate(0), theDate(1), theDate(3))
                sprocComm.Parameters("@dateOfEmployeeExpire").Value = dateToSave
                sprocComm.Parameters("@dateOfEmployeeExpire").Value = DBNull.Value() ' Compiler does not like this assignment

            End If

    Friday, July 24, 2009 9:14 AM


  • User-481631678 posted

    your last line should be like this;

    sprocComm.Parameters("@dateOfEmployeeExpire").Value = DBNull.Value

    no need to ad paranthesis in DBNull.Value

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 24, 2009 9:18 AM