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

前端 未结 2 1632
滥情空心
滥情空心 2020-12-21 01:37

I am getting this exception from the following VB.NET code for only certain months:

System.InvalidCastException: Conversion from type \'DBNull\' to type \'Da         


        
相关标签:
2条回答
  • 2020-12-21 01:52

    check for null with dRow.IsNull("CompleteDate") before you ask for the value.

    Or if this is 'supposed to be' impossible, change your query to never return rows where it can be null.

    0 讨论(0)
  • 2020-12-21 01:54

    I use a sub (different for each datatype) to get data from a Datareader, using the index instead of the name though:

    #If Access Then
    <Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
    Friend Function GetDbStringValue(ByVal Dr As OleDbDataReader, ByVal nr As Integer) As String
    #Else
    <Global.System.Diagnostics.DebuggerStepThroughAttribute()> _
    Friend Function GetDbStringValue(ByVal Dr As MySqlDataReader, ByVal nr As Integer) As String
    #End If
        If IsDBNull(Dr.Item(nr)) Then
            Return ""
        Else
            Return Dr.GetString(nr).TrimEnd
        End If
    End Function
    
    0 讨论(0)
提交回复
热议问题