Question

I have looked in other threads with this similar issue but haven't found my answer yet.

The code selects items from a DataGridView and places them into textboxes for easy reading. I get this error if once of the dates hasnt been completed. The date is nullable in SQL...

Here is my code... trying to deal with it.

If Not DataGridView.CurrentRow.Cells(9).Value() Is Nothing Then
    txtduetoend.Text = CType(DataGridView.CurrentRow.Cells(9).Value(), DateTime).ToString("dd/MM/yyyy")
End If

It's not working..

Any ideas?

Was it helpful?

Solution

I would use a variable because it's more readable and less error-prone:

Dim obj = DataGridView.CurrentRow.Cells(9).Value
Dim dueToEnd As Date? = Nothing
If obj IsNot Nothing AndAlso Not DBNull.Value.Equals(obj) Then dueToEnd = CType(obj, Date?)
If dueToEnd.HasValue Then
    txtduetoend.Text = dueToEnd.Value.ToString("dd/MM/yyyy")
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top