Pergunta

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

Como faço para verificar se o Date coluna de tipo para cada linha no DataTable está vazia?

tentei

    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

mas todos eles resultaram no Cannot cast DBNull.Value to type 'System.DateTime'. Please use a nullable type. durante o tempo de execução.

Foi útil?

Solução

Use um Anulável tipo

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 use IsNull

If Not row.IsNull("DOB") Then
    dob_str = row.Field(Of Date)("DOB")
End If
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top