如何在.NET中处理null DateTime并将它们转换为指定的DateTime?

时间:2015-02-04 03:50:34

标签: vb.net oracle

在最后一行中,它会给出错误"年,月和日参数描述不可表示的日期时间。"当dr.item(" t $ ltdt")等于null时。

如何处理null DateTime并将它们转换为指定的DateTime?

Dim sqlCur As String = "SELECT * FROM baan.ttiitm001800  WHERE (t$kitm ='1' or t$kitm='4') " & ItemCondtion & "  ORDER BY t$item"

    Dim cmd As New OracleCommand(sqlCur, connBaan)
    Dim dr As OracleDataReader = cmd.ExecuteReader()

    If dr.HasRows Then
        While dr.Read()
            Dim ChkItmCd As String = "SELECT Item_Code FROM MPC_Item_Master WHERE Item_Code='" & Trim(dr.Item("t$item")) & "' "
            db.OpenTable(ChkItmCd)

            If db.Eof Then
                ItmType = Trim(dr.Item("t$kitm"))   

                If (Trim(dr.Item("t$kitm")) = "1") Then
                    ItmType = "PURCHASED"
                ElseIf (Trim(dr.Item("t$kitm")) = "4") Then
                    ItmType = "COST"
                End If

                Dim ltdt As Date = dr.Item("t$ltdt")

1 个答案:

答案 0 :(得分:0)

您可能想要使用Convert.ToDateTime method。它采用多种格式并将其转换为DateTime。

另外,请确保您的输入不为空:

Dim ltdt As Date
If dr.Item("t$ltdt") IsNot Nothing Then
    ltdt = Convert.ToDateTime(dr.Item("t$ltdt"))
End If