vb.netdate

check if Date is null and if so, insert a new value


I have a Class Property called PasswordDate. It's being updated via a stored procedure. PasswordDate is a Date, not a DateTime. It's returning null and VB.Net is throwing an error:

Conversion from type 'DBNull' to type 'Date' is not valid.

It's throwing an error every place I use PasswordDate in my StackTrace.

This is my test user and the column PasswordDate was just created, so my user doesn't have a date for that column.

So, what my plan is, check to see if it's null, if it is, make Password = Date.Now()

I have tried:

If (PasswordDate Is Nothing) Then

Also:

If Date.TryParse(ua_PasswordDate_, passwordDate) Then

I have looked at these SO questions: Question 1 and Question 2


Solution

  • To check if a value from a database is null you can use IsDBNull(), and it works for date variables as well:

    If IsDBNull(PasswordDate) Then
        '....
    End if