1

我搜索了高低都无济于事,这是完成我的项目之前的最后一步,所以请帮忙!提前致谢!

用户将在 gridview 中选择一个条目,然后将它们重定向到一个填充有来自所选行的数据的表单(从而使 gridview 以更用户友好的方式可编辑)。空值被数据库接受,我想在相应的文本框中将空日期值显示为空白(或“”)。相反,我得到了错误:

从类型“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
4

2 回答 2

2

try this when you read the values from the reader with all your dates, this will first test to see if the date is dbnull, if it is then it will assign a nothing value and you should get your desired empty cell, otherwise it will show the date:

ucistart = IIf(reader3("UCIStartDate") Is DBNull.Value, Nothing, reader3("UCIStartDate"))
于 2011-06-24T05:13:17.203 回答
0

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

Here is the official documentation.

于 2011-06-24T05:10:34.047 回答