溢出异常错误

时间:2012-08-18 04:37:43

标签: asp.net vb.net

这是在asp.net应用程序中

我有这个例外 System.OverflowException: Arithmetic operation resulted in an overflow。 但奇怪的是,当调用函数并返回结果时,这个错误似乎浮出水面 但如果我直接编写功能代码而不是通过函数编写,只需在代码下面写下它就不会出现此错误 善意的帮助 这是代码

Dim MyItem As New Item    
If MyItem.GetQtyInBin(bran_code.Text, itcode.Text, binloc.Text) <= 0 Then

    ...............

    Exit Sub

这是方法的代码

'this method gets the qty of a certain item in a certainn location and returns it
 Public Function GetQtyInBin(ByVal Branch As String, ByVal Item As Integer, ByVal Bin As String) As Integer

    cn = New OleDb.OleDbConnection("provider=msdaora; Data Source=llmw; User Id=xxxx;Password=xxxx")
    cmd = New OleDb.OleDbCommand("select Qty from Binprmast where Bin_No='" & Bin & "' and item_Code='" & Item & "' and Bran_Code='" & Branch & "'", cn)
    cn.Open()
    Dim qty As Double
    qty = cmd.ExecuteScalar
    Return qty

End Function

2 个答案:

答案 0 :(得分:0)

在您的问题中,GetQtyInBin定义为返回Integer,但返回值qty定义为Double

Double大于整数,因此溢出。

像这样纠正

Public Function GetQtyInBin(ByVal Branch As String, ByVal Item As Integer, ByVal Bin As String) As Double
        cn = New OleDb.OleDbConnection("provider=msdaora; Data Source=llmw; User Id=boss;Password=boss")
        cmd = New OleDb.OleDbCommand("select Qty from Binprmast where Bin_No='" & Bin & "' and item_Code='" & Item & "' and Bran_Code='" & Branch & "'", cn)
        cn.Open()
        Dim qty As Double
        qty = cmd.ExecuteScalar
        Return qty
    End Function

答案 1 :(得分:0)

您的函数返回一个整数,但您将返回值设置为Double。您需要更改其中一个以匹配。

注意:如果您switch Option Strict On这将不会按原样编译,因此会提醒您出现此错误。