How do I test for null date in SQL table?

Visual Basic 2005 Express:

SQL won't permit null dates in a table. I would like to test for this condition and replace the null date with a DefaultDate (01/01/1901). How do I test for a null date in an SQL table The following code gives an error saying that DBNull is a type and cannot be used in an expression.

If PatientBindingSource.Current("BirthDate") = DBNull Then
PatientBindingSource.Current("BirthDate")=DefaultDate
End If



Answer this question

How do I test for null date in SQL table?

  • DanielMac

    Yes, I entered a default value in the properties window for BirthDate.

    I also have the following statement in the Public area of the program:

    Dim DefaultDate as date = "01/01/1901"


  • HG71

    The isdbnull method in VB.Net seems to do the trick.

     If Isdbnull(PatientBindingSource.Current("BirthDate") ) Then
       PatientBindingSource.Current("BirthDate") =DefaultDate
    end if

     

    Or, You can use a statement such as

     If PatientBindingSource.Current("BirthDate"Is DBNull.Value  Then
       PatientBindingSource.Current("BirthDate") =DefaultDate
    end if


  • kenneth123

    Surprisingly, I still get the Null Value error!

    Public Sub PatientBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As       System.EventArgs) Handles PatientBindingNavigatorSaveItem.Click
      
    If IsDBNull(PatientBindingSource.Current("BirthDate")) Then
          PatientBindingSource.Current("BirthDate") = DefaultDate
       End If
       Me.Validate()
       Me.PatientBindingSource.EndEdit()
       Me.PatientTableAdapter.Update(Me.PatientDataSet.Patient)
    End Sub

     


  • Jayakumar A

    Have you initialised DefaultDate



  • Ed1112ward

    Did you check the field values in the dataset and the database - did you make the default value change after you created the dataset

    Changing the database field will not automatically update the dataset and vice versa.

    Also if you seeing a value in the field then its probably not a dbnull that you need to test for.


  • Malic Huang

    Do the testing for dbnull methods work if you code step through them. When you get to the line of code and you look at the value of the field is it showing as dbnull

    Yes, I entered a default value in the properties window for BirthDate.

    Was this in the dataset and the database.


  • Gabrielle Erlingen

    When I get to the line of code and I look at the value of the field, it is showing as "01/01/1901".
  • How do I test for null date in SQL table?