-3

我创建了一个名为的类Cls_ICCID,在其中声明了Update_Status返回 byRef 变量的方法。


Cls_ICCID

Public Sub Update_Status(**ByRef massege As String**, ByVal ICCID_No As Integer, ByVal status As Integer)
        Try
            Dim cmd As SqlCommand
            Dim sql As String
            Dim myConnection As SqlConnection = New SqlConnection()
            myConnection.ConnectionString = "Data Source=TEHRANI\TEHRANI;Initial Catalog=GSMProduction;Persist Security Info=True;User ID=sa;Password=1"
            sql = "UPDATE Tbl_ICCID SET Status=status WHERE ICCID=ICCIDNo"
            myConnection = New SqlConnection(sql)
            myConnection.Open()
            cmd = New SqlCommand(sql, myConnection)
            cmd.ExecuteNonQuery()
            cmd.Dispose()
            myConnection.Close()
            massege = "SeccessFully"
        Catch ex As Exception
            massege = "server Error"
        End Try
    End Sub

然后在触发文本框更改事件时执行该方法:

 Private Sub Txt_ICCID_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Txt_ICCID.TextChanged
     Dim clsICCID As Cls_ICCID
     clsICCID.Update_Status(lblError.Text, Txt_ICCID.Text, 1)
 End Sub

但这给了我一个例外:

算术运算导致溢出。 在此处输入图像描述 我究竟做错了什么?

4

4 回答 4

1

老实说,正如 konrad 所说,如果您想使用此类代码,您应该真正研究基础知识。

还没有尝试过代码,但我认为你想要类似的东西

Public Function Update_Status(iccidNo As Integer, status As Integer) As String
    Const sql As String = "UPDATE Tbl_ICCID SET Status=@status WHERE ICCID=@ICCIDNo"
    Const connstr As String = "Data Source=TEHRANI\TEHRANI;Initial Catalog=GSMProduction;Persist Security Info=True;User ID=sa;Password=1"
    Try
        Using myConnection = New SqlConnection()
            myConnection.ConnectionString = connstr
            Using cmd = New SqlCommand(sql, myConnection)
                cmd.Parameters.AddWithValue("@status", status)
                cmd.Parameters.AddWithValue("@ICCIDNo", iccidNo)
                cmd.ExecuteNonQuery()
                Update_Status = "Successfully"
            End Using
        End Using
    Catch ex As SqlException
        Update_Status = "server Error"
    Catch ex As Exception
        Update_Status = "server Error"
    End Try
End Function

但就我个人而言,我不会那样处理异常并返回一个布尔值。

于 2013-02-18T11:13:20.357 回答
1

您特别要求的是在方法中返回值。而不是Sub你必须声明它Function,然后返回值return

Public Function Update_Status(ICCID_No As Integer, status As Integer) As String
    '...

    return massege
End Function

然后抓住价值:

lblError.Text = clsICCID.Update_Status(Txt_ICCID.Text, 1)
于 2013-02-18T11:14:04.140 回答
0

我应该使用 from Function 而不是 sub 方法。因为 sub 方法是无效的。

于 2013-02-18T11:13:14.793 回答
0

对于返回值的方法,您需要将方法声明为函数,而不是 sub。例如:

Public Function Update_Status(ByVal ICCID_No As Integer, ByVal status As Integer) As String
    Return "errorMessage"
End Function

然后可以像这样调用它:

returnValue = Update_Status(Txt_ICCID.Text, 1)

见:http: //msdn.microsoft.com/en-us/library/sect4ck6%28v=vs.80%29.aspx

但似乎您的参数可能不匹配:将字符串作为整数发送?

于 2013-02-18T11:18:11.060 回答