vérifier si la colonne de date dans datatable est nulle
Question
Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Dim dt As DataTable = New DataTable()
dt.Load(dr)
For Each row As DataRow In dt.Rows
Dim dob_str As String = ""
'code to check if row.Field(Of Date)("DOB") is dbnull
'if not dbnull then dob_str = row.Field(Of Date)("DOB")
Next
Comment puis-je vérifier si le Date
tapez une colonne pour chaque ligne du DataTable
est vide?
J'ai essayé
If Not row.Field(Of Date)("DOB") = Nothing Then
dob_str = row.Field(Of Date)("DOB")
End If
et
Dim nullCheck As Boolean = IsDBNull(row.Field(Of Date)("DOB"))
If nullCheck = False Then
dob_str = row.Field(Of Date)("DOB")
End If
mais ils ont tous abouti au Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type.
pendant l'exécution.
La solution
Utiliser un Nullable taper
If row.Field(Of Date?)("DOB") IsNot Nothing Then
dob_str = row.Field(Of Date)("DOB")
End If
Ou
If row.Field(Of Date?)("DOB").HasValue Then
dob_str = row.Field(Of Date)("DOB")
End If
Ou utiliser IsNull
If Not row.IsNull("DOB") Then
dob_str = row.Field(Of Date)("DOB")
End If
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow