Controlla se la colonna della data in Datatabili è null
Domanda
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
.
Come posso controllare se la colonna tipo Date
per ogni riga nel DataTable
è vuoto?
Ho provato
If Not row.Field(Of Date)("DOB") = Nothing Then
dob_str = row.Field(Of Date)("DOB")
End If
.
e
Dim nullCheck As Boolean = IsDBNull(row.Field(Of Date)("DOB"))
If nullCheck = False Then
dob_str = row.Field(Of Date)("DOB")
End If
.
Ma tutti hanno comportato il Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type.
durante il runtime.
Soluzione
Utilizzare un nullble digita
If row.Field(Of Date?)("DOB") IsNot Nothing Then
dob_str = row.Field(Of Date)("DOB")
End If
.
o
If row.Field(Of Date?)("DOB").HasValue Then
dob_str = row.Field(Of Date)("DOB")
End If
.
o utilizzare IsNull
If Not row.IsNull("DOB") Then
dob_str = row.Field(Of Date)("DOB")
End If
. Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow