正在将空字符串转换为日期



我到处找都找不到,这是完成我的项目前的最后一步,所以请帮忙!提前感谢!

用户将在网格视图中选择一个条目,然后将它们重定向到一个用所选行中的数据填充的表单(从而使网格视图以更用户友好的方式进行编辑)。DB接受空值,我希望在相应的文本框中将空日期值显示为空(或")。相反,我得到了错误:

从类型"DBNull"到类型"Date"的转换无效。

这是我的代码:

'preceded by connection code     
Dim sqlcmd As String = "SELECT * from Master WHERE RecNum = @recnum"
        'Dim sqlCmd As New OleDb.OleDbCommand("SELECT * from Master WHERE RecNum = @recnum", connection)
        Dim FileCommand3 As New OleDb.OleDbCommand(sqlcmd, connection)
        FileCommand3.Parameters.AddWithValue("@recnum", user)
        Dim Reader3 As OleDb.OleDbDataReader = FileCommand3.ExecuteReader()
        If Reader3.Read Then
            stock = myCStr(Reader3("StockNum"))
            make = myCStr(Reader3("Make"))
            color = myCStr(Reader3("Color"))
            stockin = myCStr(Reader3("Stockin"))
            ucistart = myCStr(Reader3("UCIStartDate"))
            repairs = Reader3("Repairs")
            tires = Reader3("tiresneeded")
            onlot = Reader3("onlot")
            sold = Reader3("sold")
            year = myCStr(Reader3("year"))
            model = myCStr(Reader3("model"))
            location = Reader3("location")
            srvcRO = myCStr(Reader3("svcROnum"))
            ucicompldate = myCStr(Reader3("uciestcompletedate"))
            collRO = myCStr(Reader3("collisionROnum"))
            other = myCStr(Reader3("other"))
            offprop = Reader3("offProperty")
            detail = (Reader3("detail")
        End If
        connection.Close()
        SoldCheckBX.Checked = sold
        DetailTXTbox.Text = detail
        'etc, etc
    End Sub

我使用函数mycstr来修复dbnull到字符串的错误,但适应"日期"数据类型似乎并不简单

Function myCStr(ByVal test As Object) As String
    If isdbnull(test) Then
        Return ("")
    Else
        Return CStr(test)
    End If
End Function

当你从阅读器上读取所有日期的值时,试试这个,这将首先测试日期是否为dbnull,如果是,它将分配一个nothing值,你应该得到你想要的空单元格,否则它将显示日期:

ucistart = IIf(reader3("UCIStartDate") Is DBNull.Value, Nothing, reader3("UCIStartDate"))

您是否尝试过使用Convert.IsDBNull函数?

这是官方文件。

相关内容

  • 没有找到相关文章

最新更新