【问题标题】:Why is IsDBNull throwing a conversion overflow exception?为什么 IsDBNull 会抛出转换溢出异常?
【发布时间】:2021-09-25 08:56:06
【问题描述】:

我在这里有一个非常简单的代码 sn-p,这给我带来了很多问题。我还想提一下,应该使用 using 语句,但代码最初不是由我编写的。我可能会在某个时候解决这个问题

            Dim dbShippableOpenOrders As New sqlCommand(sqlText, objConn)
            Dim rsShippableOpenOrders As sqlDataReader = dbShippableOpenOrders.executeReader

...


                If IsDBNull(rsShippableOpenOrders("cost")) Then
                    cost = 0
                Else
                    cost = rsShippableOpenOrders("cost")
                End If

我似乎无法弄清楚为什么这段代码会产生以下错误消息:

转换溢出。
描述:发生未处理的异常 在当前 Web 请求执行期间。请查看 堆栈跟踪以获取有关错误及其位置的更多信息 源于代码。

异常详情:System.OverflowException:转换溢出。

来源错误:

Line 355:                End If 
Line 356: 
Line 357:                If IsDBNull(rsShippableOpenOrders("cost")) Then 
Line 358:                    cost = 0 
Line 359:                Else

【问题讨论】:

  • 我不知道你所问问题的答案,但你不应该一开始就使用IsDBNull 方法。数据读取器有自己的方法来检测 NULL,因此请使用:cost = If(rsShippableOpenOrders.IsDBNull(rsShippableOpenOrders.GetOrdinal("cost")), 0, rsShippableOpenOrders.GetInt32(rsShippableOpenOrders.GetOrdinal("cost")))
  • 顺便问一下,您真的确认IsDBNull 是问题所在吗?您应该已经这样做以确认但尝试先获取字段值并将其分配给变量,然后在调试器中查看它以查看它实际包含的内容。它有效,然后您可以将该变量传递给IsDBNull。如果您是正确的,那么第二次操作将引发异常,但我怀疑它会。
  • @jmcilhinney 这是在旧的 IIS 服务器上运行的,我无法访问任何调试或服务器级别的任何内容。我唯一能做的就是查看数据库并编辑这些旧的 aspx 文件。另外,cost 必须是浮点值,不能是 int。
  • cost 声明在哪里?
  • “这是在旧的 IIS 服务器上运行的,我无法访问任何调试或服务器级别的任何内容。我唯一能做的就是查看数据库并编辑这些旧的 aspx 文件。在那种“斯巴达式”条件下,您可以在该服务器中创建一个文件夹完全权限读/写。日志文件对您来说总是一个加分项。

标签: asp.net vb.net


【解决方案1】:

尝试以下方法:

Dim cost As Decimal = 0
Dim dbShippableOpenOrders As New SqlCommand(sqlText, objConn)
Dim rsShippableOpenOrders As SqlDataReader = dbShippableOpenOrders.ExecuteReader()

If rsShippableOpenOrders.HasRows Then
    While rsShippableOpenOrders.Read()
        If rsShippableOpenOrders("cost") IsNot Nothing AndAlso Not IsDBNull(rsShippableOpenOrders("cost")) Then
            cost = CDec(rsShippableOpenOrders("cost"))
        Else
            cost = 0
        End If
    End While
End If

这是一个示例方法:

Public Sub GetDataTblProduct(connectionStr As String)

    Dim sqlText As String = "Select * from Product"
    Dim cost As Decimal = 0

    Using con As SqlConnection = New SqlConnection(connectionStr)
        'open
        con.Open()

        Using dbShippableOpenOrders As SqlCommand = New SqlCommand(sqlText, con)
            Using rsShippableOpenOrders As SqlDataReader = dbShippableOpenOrders.ExecuteReader
                If rsShippableOpenOrders.HasRows Then
                    While rsShippableOpenOrders.Read()
                        If rsShippableOpenOrders("cost") IsNot Nothing AndAlso Not IsDBNull(rsShippableOpenOrders("cost")) Then
                            cost = CDec(rsShippableOpenOrders("cost"))
                        Else
                            cost = 0
                        End If

                        'Debug.WriteLine("cost: " & cost)
                    End While
                End If
            End Using
        End Using
    End Using
End Sub

【讨论】:

    猜你喜欢
    • 2015-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-11
    • 2021-04-28
    • 2013-09-04
    相关资源
    最近更新 更多