如何解决错误从“DBNull”类型转换为“Date”类型无效。 [英] How to resolve the error Conversion from type 'DBNull' to type 'Date' is not valid.

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

问题描述

  Dim  InveststrBuilder1 作为  StringBuilder 
如果修剪(RSmartUtl.DtFormat)= < span class =code-string> DD-MM-YYYY 然后
InveststrBuilder1.Append( document.forms [0] .INVEST_POLICY_DATE_DD.value = unescape('& Format(DatePart(DateInterval.Day,objDR( DOC_DATE)), 00)& ');& ; vbCrLf)
InveststrBuilder1.Append( document.forms [0] .INVEST_POLICY_DATE_MM.va lue = unescape('&格式(DatePart(DateInterval.Month,objDR( DOC_DATE)), 00)& ');& vbCrLf)
结束 如果
如果修剪(RSmartUtl.DtFormat)= MM-DD-YYYY 然后
InveststrBuilder1.Append( document.forms [0 ] .INVEST_POLICY_DATE_DD.value = unescape('& Format(DatePart(DateInterval.Month,objDR( DOC_DATE)), 00)& ');& vbCrLf)
InveststrBuilder1.Append( document.forms [0] .INVEST_POLICY_DATE_MM.value = unesca pe('&格式(DatePart(DateInterval.Day,objDR( DOC_DATE)), 00)& ');& vbCrLf)
结束 如果
InveststrBuilder1.Append( document.forms [0] .INVEST_POLICY_DATE_YY.value = unescape('& Format(DatePart(DateInterval.Year,objDR( DOC_DATE)), < span class =code-string> 0000)& ');& vbCrLf)

当数据库中Date为null时,即后端我收到错误

从类型'DBNull'转换为'Date'类型无效。

解决方案

检查 DBNull.Value [ ^ ]在开始处理日期之前...


objDR(DOC_DATE)包含从数据库中检索的数据不是吗?在数据库中,相应的列可能包含NULL,当您将数据退出到.Net程序时,它会被转换为DBNull。

然后DatePart函数尝试将其转换为DateTime变量并且失败:DateTime是一个值类型,不能为null。

因此在执行该处理之前检查objDR(DOC_DATE)是否为DBNull.Value,正如Macej告诉你的那样:

 如果 objDR(  DOC_DATE)= DBNull.Value 然后 
MessageBox.Show( 没有给出日期!
其他
' 上面的代码
EndIf


Dim InveststrBuilder1 As New StringBuilder
 If Trim(RSmartUtl.DtFormat) = "DD-MM-YYYY" Then
                                    InveststrBuilder1.Append("document.forms[0].INVEST_POLICY_DATE_DD.value=unescape('" & Format(DatePart(DateInterval.Day, objDR("DOC_DATE")), "00") & "');" & vbCrLf)
                                    InveststrBuilder1.Append("document.forms[0].INVEST_POLICY_DATE_MM.value=unescape('" & Format(DatePart(DateInterval.Month, objDR("DOC_DATE")), "00") & "');" & vbCrLf)
                                End If
                                If Trim(RSmartUtl.DtFormat) = "MM-DD-YYYY" Then
                                    InveststrBuilder1.Append("document.forms[0].INVEST_POLICY_DATE_DD.value=unescape('" & Format(DatePart(DateInterval.Month, objDR("DOC_DATE")), "00") & "');" & vbCrLf)
                                    InveststrBuilder1.Append("document.forms[0].INVEST_POLICY_DATE_MM.value=unescape('" & Format(DatePart(DateInterval.Day, objDR("DOC_DATE")), "00") & "');" & vbCrLf)
                                End If
                                InveststrBuilder1.Append("document.forms[0].INVEST_POLICY_DATE_YY.value=unescape('" & Format(DatePart(DateInterval.Year, objDR("DOC_DATE")), "0000") & "');" & vbCrLf)

When Date is null in database i.e back end I get the error
Conversion from type 'DBNull' to type 'Date' is not valid.

解决方案

Check for DBNull.Value[^] before you start processing with date...


objDR("DOC_DATE") contains data retrieved from a database, doesn't it? And there in the database, the corresponding column may contain NULL, which gets translated into DBNull when you retireve the data into your .Net program.
The DatePart function then tries to cast that into a DateTime variable and fails: DateTime is a value type and must not be null.
Hence check for objDR("DOC_DATE") being DBNull.Value before you do that processing, as Macej told you:

If objDR("DOC_DATE") = DBNull.Value Then
    MessageBox.Show("No date given!")
Else
    'your code from above
EndIf


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

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