将空字符串转换为日期

发布于 2024-11-16 23:25:13 字数 1928 浏览 0 评论 0原文

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

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

从“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 to string 错误,但适应“date”数据类型似乎并不那么简单

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

I have searched high and low to no avail, and this is last step before completing my project so please help! Thanks in advance!

The user will select an entry in a gridview, which then redirects them to a form that is populated with the data from the selected row (thus making the gridview editable in a more user friendly way). Null values are accepted by the DB and I would like to show null date values as blank (or " ") in the corresponding text boxes. Instead I get the error:

Conversion from type 'DBNull' to type 'Date' is not valid.

Here is my code:

'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

I used the function mycstr to fix the dbnull to string error but it does not seem as simple to adapt to "date" data type

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

╭ゆ眷念 2024-11-23 23:25:13

当您从读取器读取所有日期的值时,请尝试此操作,这将首先测试日期是否为 dbnull,如果是,则它将分配一个空值,您应该得到所需的空单元格,否则它将显示日期:

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

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"))
伴我心暖 2024-11-23 23:25:13

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

这里是官方文档。

Have you tried using the Convert.IsDBNull function?

Here is the official documentation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文