I am loading a value into a txtbox(driver) from our sql database using another a filled txtbox(car registration) as a filter.
So I will be able to get the right driver for that vehicle. As each vehicle can have a number of people driving it in its time, I am using an IIf clause to make sure the current driver is selected.
I am getting this error:
Operator '>' is not defined for type 'DBNull' and type 'Date'.
I know I need to convert the DBNull to a date format, but am struggling to do so, I have a tried a few things, and so far this is where I have got to.
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
Any help would be greatly appreciated
Paul
So row("end_date")
is nullable? You can use the DataRow
-extension method Field
which supports nullable types and is strongly typed:
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