我搜索了高低都无济于事,这是完成我的项目之前的最后一步,所以请帮忙!提前致谢!
用户将在 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