The stored procedure returns information about the user record, including a column with a zero column of date and time for their last entry date. Which one is the best choice when considering the possibility of NULL values when trying to assign a .Net date variable?
Try
_LastLogin = CDate(DT.Rows(0)("LastLogin"))
Catch ex As InvalidCastException
_LastLogin = Nothing
End Try
or
If DT.Rows(0)("LastLogin") Is DBNull.Value Then
_LastLogin = Nothing
Else
_LastLogin = CDate(DT.Rows(0)("LastLogin"))
End If
Change . I also forgot about the possibility of using TryParse
If Not Date.TryParse(DT.Rows(0)("LastLogin").ToString, _LastLogin) Then
_LastLogin = Nothing
End If
What is the preferred method for processing possible values NULLfrom a database? Is there a better way than the three listed?
Change # 2 . I noticed that the method TryParsedoes not work well when trying to assign a type Nullable.
source
share