我正在使用另一个填充的txtbox(汽车注册)作为过滤器从我们的sql数据库将值加载到txtbox(驱动程序)中。
所以我将能够获得该车辆的正确驾驶员。由于每辆车都有许多人在当时驾驶它,我使用IIf子句来确保选择当前的驱动程序。
我收到此错误:
运营商'>'未定义类型“DBNull”并键入“日期”。
我知道我需要将DBNull转换为日期格式,但我正在努力这样做,我尝试了一些事情,到目前为止,这是我必须要做的事情。
Dim cdriver As New Dictionary(Of String, Object)
cdriver.Add("Registration", txtreg.Text)
Dim currentdriver As DataTable
currentdriver = BusinessData.VerifierLogic.Load("[Car History]", cdriver)
Dim currentdrivername As String = ""
Dim currentenddate As DateTime
For Each row As DataRow In currentdriver.Rows
If DateDiff(DateInterval.Day, row("end_date"), DateTime.Now) > 0 Then
currentdrivername = row("driver_name")
currentenddate = row("end_date")
IIf(row("end_date") Is DBNull, "1 jan 1900", row("end_date"))
End If
Next
txtdriver.Text = currentdrivername
Catch ex As Exception
MsgBox(ex.Message)
End Try
非常感谢任何帮助
保
答案 0 :(得分:2)
所以row("end_date")
可以为空吗?您可以使用支持可空类型的DataRow
- 扩展方法Field
,并且是强类型的:
For Each row As DataRow In currentdriver.Rows
Dim endDate As Date? = row.Field(Of Date?)("end_date")
If endDate.HasValue AndAlso endDate.Value > Date.Now Then
currentdrivername = row.Field(Of String)("driver_name")
currentenddate = endDate.Value
Exit For ' otherwise you are overwriting these variables always '
End If
Next
答案 1 :(得分:0)
必须为Tim Schmelter提供帮助。
我必须稍微改变一下才能实现它,但代码的核心是你的。这是完成的文章:
For Each row As DataRow In currentdriver.Rows
Dim endDate As Date? = row.Field(Of Date?)("end_date")
If Not endDate.HasValue Then
currentdrivername = row.Field(Of String)("driver_name")
ElseIf endDate.Value > Date.Now Then
currentdrivername = row.Field(Of String)("driver_name")
End If