另一个"conversion from type DBNull to type Date not valid"问题



我已经在其他线程中查看了这个类似问题,但尚未找到我的答案。

代码从DataGridView选择项目,然后将其放入文本框中以方便阅读。如果一旦日期尚未完成,我会遇到此错误。该日期在SQL ...

中是无效的

这是我的代码...试图处理它。

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

它不起作用..

有什么想法?

我会使用一个变量,因为它更可读性且容易出错:

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

相关内容

最新更新