从“DBNull”类型转换为“Date”类型无效 [英] Conversion from type 'DBNull' to type 'Date' is not valid

查看:495
本文介绍了从“DBNull”类型转换为“Date”类型无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  System.InvalidCastException:从...转换键入'DBNull'键入'Date'无效。 

它发生在线上:

 如果CDate(dRow(CompleteDate))ToString(d)= arrWeekYear(i,1).ToString(d)然后

如果我正确理解这一点,那么问题是我试图将CompleteDate的一些NULL值与非NULL值进行比较。 >

如何解决这个问题?在某些日期,上面的代码运行完美。然而,对于一些最近的记录,它没有。但是我可以从相同日期范围的T-SQL查询中获取输出,并且没有错误;它也很快运行。而且我检查了Date_Completed和Review_DateNULL值的这个T-SQL查询,但是无论如何,CompleteDate总是= NON-Null值。所以我不明白这是怎么回事。



这是我的VB.NET查询:

  commandstring =SELECT Batch_Records.Part_Number,Batch_Records.Lot_Number,Batch_Records.Date_Received,& _ 
IsNull([Date_Completed],[Review_Date])AS [CompleteDate],Batch_Records.Error,& _
Batch_Records。[Group],Batch_Records.MFG,Batch_Records.MFG2,Batch_Records.QC,Batch_Records.QC2,& _
QC_CODES.CODE_DESC,DATEADD(DD,1 - DATEPART(DW,Batch_Records.Date_Received),Batch_Records.Date_Received)AS SundayDate& _
FROM EXCEL.Batch_Records LEFT JOIN EXCEL.QC_CODES ON Batch_Records.Part_Number = QC_CODES.CODE_ID& _
WHERE(Batch_Records。[Group]& TheGroup&AND Batch_Records.Date_Received>'& FirstWeek&'AND Batch_Records.Date_Received< dateadd(Day,1,'& ; LastWeek&'))& _
ORDER BY Batch_Records.Date_Received

当我在错误 - 导致上述情况,我的报告超出了这几个月。那么该怎么办?

 如果不是dRow(CompleteDate)是System.DBNull.Value然后


解决方案

在请求之前,使用dRow.IsNull(CompleteDate)检查null



或者如果这是不可能,请更改您的查询,从不返回可以为空的行。


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

System.InvalidCastException: Conversion from type 'DBNull' to type 'Date' is not valid.

It happens on the line:

If CDate(dRow("CompleteDate")).ToString("d") = arrWeekYear(i, 1).ToString("d") Then

If I understand this correctly, then the problem is I am trying to compare some NULL values for CompleteDate to a non-NULL value.

How do I fix this? For some dates, this code above runs perfectly. However for some of the more recent records, it does not. But I am able to get output from the T-SQL query with same date range and there are no errors; it runs quickly also. And I examined this T-SQL query for both "Date_Completed" and "Review_Date" NULL values, but either way, "CompleteDate" was always = NON-Null value. So I do not understand how this is happening.

Here is my VB.NET query:

            commandstring = "SELECT Batch_Records.Part_Number, Batch_Records.Lot_Number, Batch_Records.Date_Received, " & _
                "IsNull([Date_Completed], [Review_Date]) AS [CompleteDate], Batch_Records.Error, " & _
                "Batch_Records.[Group], Batch_Records.MFG, Batch_Records.MFG2, Batch_Records.QC, Batch_Records.QC2, " & _
                "QC_CODES.CODE_DESC, DATEADD(DD, 1 - DATEPART(DW, Batch_Records.Date_Received), Batch_Records.Date_Received) AS SundayDate " & _
                "FROM EXCEL.Batch_Records LEFT JOIN EXCEL.QC_CODES ON Batch_Records.Part_Number = QC_CODES.CODE_ID " & _
                "WHERE (Batch_Records.[Group]" & TheGroup & " AND Batch_Records.Date_Received > '" & FirstWeek & "' AND Batch_Records.Date_Received < dateadd(Day, 1, '" & LastWeek & "'))" & _
                   "ORDER BY Batch_Records.Date_Received"

When I add this line above the error-causing line above, my report times out for these months. So what to do?

If Not dRow("CompleteDate") Is System.DBNull.Value Then

解决方案

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.

这篇关于从“DBNull”类型转换为“Date”类型无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆