Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 捕获类文件到aspx.vb页面的异常_C#_Asp.net_Vb.net - Fatal编程技术网

C# 捕获类文件到aspx.vb页面的异常

C# 捕获类文件到aspx.vb页面的异常,c#,asp.net,vb.net,C#,Asp.net,Vb.net,此函数位于App\u code文件夹中的类文件中 Public Shared Function CRUD(ByVal _sql As String, ByVal _parameterNames() As String, ByVal _parameterVals() As String) As Integer Dim _noOfRowsAffected As Integer Dim _connection As SqlConnection = Global.Con

此函数位于
App\u code
文件夹中的类文件中

 Public Shared Function CRUD(ByVal _sql As String, ByVal _parameterNames() As String, ByVal _parameterVals() As String) As Integer
        Dim _noOfRowsAffected As Integer
        Dim _connection As SqlConnection = Global.Connection.GetDbConnection()
        Dim _command As New SqlCommand(_sql, _connection)

        Try
            If _parameterNames IsNot Nothing Then
                For i = 0 To _parameterNames.Length - 1
                    _command.Parameters.AddWithValue(_parameterNames(i), _parameterVals(i))
                Next
            End If

            _noOfRowsAffected = _command.ExecuteNonQuery()
        Catch ex As Exception
            'MsgBox(ex.Message)
            _noOfRowsAffected = -1
        Finally
            If _connection.State = ConnectionState.Open Then
                _connection.Close()
                _connection.Dispose()
                _command.Dispose()
            End If
        End Try

        Return _noOfRowsAffected
    End Function
此代码位于aspx.vb页面中

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim _parameterNames(6), _parameterVals(6) As String
        _parameterNames(0) = "@Name"
        _parameterVals(0) = txtCoachingName.Text
        _parameterNames(1) = "@Address"
        _parameterVals(1) = txtCoachingAddress.Text
        _parameterNames(2) = "@Mob1"
        _parameterVals(2) = txtCoachingMob1.Text
        _parameterNames(3) = "@Mob2"
        _parameterVals(3) = txtCoachingMob2.Text
        _parameterNames(4) = "@LLine"
        _parameterVals(4) = txtCoachingLLine.Text
        _parameterNames(5) = "@Established"
        _parameterVals(5) = ddlCoachingEstablished.SelectedValue
        _parameterNames(6) = "@DemoVideo"
        _parameterVals(6) = txtCoachingDemoVideo.Text
        _parameterNames(7) = "@Password"
        _parameterVals(7) = txtCoachingPassword.Text
         Try
        DataAccess.CRUD("UPDATE CoachingDetails SET Name=@Name,Address=@Address,Mob1=@Mob1,Mob2=@Mob2,L_Line=@LLine,Established=@Established,Demo_Video=@DemoVideo,Password=@Password,Step_Completed='True',Time_Stamp='" & Date.Now & "'", _parameterNames, _parameterVals)
    Catch ex As Exception

    End Try

现在我想在我的
aspx.vb
页面中捕获
主键冲突
异常..但是我在
aspx.vb
页面中没有得到异常,因为错误被类函数捕获。那么,如何从类文件获取异常到aspx.vb文件??
End Sub

BCL没有可捕获的
PrimaryKeyViolationException

最接近的方法是捕获一个
SqlException
,并查看该消息以了解SQL Server报告的问题。这方面的
Catch
块应该在
Exception
的Catch块之上,因为它更具体

Try
    If _parameterNames IsNot Nothing Then
        For i = 0 To _parameterNames.Length - 1
            _command.Parameters.AddWithValue(_parameterNames(i), _parameterVals(i))
        Next
    End If

    _noOfRowsAffected = _command.ExecuteNonQuery()
Catch ex As SqlException
    ' Do stuff - logging, checking the exception message etc...
    ' Rethrow exception if you want the exception to bubble up
Catch ex As Exception
    'MsgBox(ex.Message)
    _noOfRowsAffected = -1
Finally
    If _connection.State = ConnectionState.Open Then
        _connection.Close()
        _connection.Dispose()
        _command.Dispose()
    End If
End Try

BCL没有可捕获的
PrimaryKeyViolationException

最接近的方法是捕获一个
SqlException
,并查看该消息以了解SQL Server报告的问题。这方面的
Catch
块应该在
Exception
的Catch块之上,因为它更具体

Try
    If _parameterNames IsNot Nothing Then
        For i = 0 To _parameterNames.Length - 1
            _command.Parameters.AddWithValue(_parameterNames(i), _parameterVals(i))
        Next
    End If

    _noOfRowsAffected = _command.ExecuteNonQuery()
Catch ex As SqlException
    ' Do stuff - logging, checking the exception message etc...
    ' Rethrow exception if you want the exception to bubble up
Catch ex As Exception
    'MsgBox(ex.Message)
    _noOfRowsAffected = -1
Finally
    If _connection.State = ConnectionState.Open Then
        _connection.Close()
        _connection.Dispose()
        _command.Dispose()
    End If
End Try
。(http://www.selfelected.com/rethrow/)

既然你什么都不做,就跳过它吧

Try
    ....
Finally
    do some other stuff
End Try
为了使代码更美观,请仔细阅读。通过使用,您可以让编译器为您隐式插入Try/Finally。代码将类似于:

Using connection As SqlConnection = Global.Connection.GetDbConnection()
    Using command As New SqlCommand(sql, connection)
        If parameterNames IsNot Nothing Then
            For i = 0 To parameterNames.Length - 1
                command.Parameters.AddWithValue(parameterNames(i), parameterVals(i))
            Next
        End If
        noOfRowsAffected = _command.ExecuteNonQuery()
    End Using
End Using
请注意,我将所有变量都设置为局部变量。这对你来说可能是不可能的,因为你比我更了解你的代码。。。(但代码建议您的变量应该包含类变量,但您仍然将它们视为本地变量。)

也请查收。Dapper是堆栈溢出运行的对象。这是一个很好的小库,可以使编写sql调用更易于编写和读取。

。(http://www.selfelected.com/rethrow/)

既然你什么都不做,就跳过它吧

Try
    ....
Finally
    do some other stuff
End Try
为了使代码更美观,请仔细阅读。通过使用,您可以让编译器为您隐式插入Try/Finally。代码将类似于:

Using connection As SqlConnection = Global.Connection.GetDbConnection()
    Using command As New SqlCommand(sql, connection)
        If parameterNames IsNot Nothing Then
            For i = 0 To parameterNames.Length - 1
                command.Parameters.AddWithValue(parameterNames(i), parameterVals(i))
            Next
        End If
        noOfRowsAffected = _command.ExecuteNonQuery()
    End Using
End Using
请注意,我将所有变量都设置为局部变量。这对你来说可能是不可能的,因为你比我更了解你的代码。。。(但代码建议您的变量应该包含类变量,但您仍然将它们视为本地变量。)


也请查收。Dapper是堆栈溢出运行的对象。这是一个很好的小库,可以使编写sql调用更易于编写和读取。

如何从aspx.vb文件中获取异常???如果类文件中有try-catch块,它会捕获异常。因此,如果我在aspx.vb文件中使用try-catch块,它不会捕获任何异常。我想从aspx.vb文件访问类文件中抛出的异常。@user1150440-是。在这种情况下,您需要从catch块中重新抛出异常,这样它将冒泡到调用代码。这是我在代码注释和我给你的第一条注释中所说的。重新抛出异常不是一种糟糕的做法吗?重新抛出异常是否有任何负面影响?如何从aspx.vb文件中获取异常???如果类文件中有try-catch块,它会捕获异常。因此,如果我在aspx.vb文件中使用try-catch块,它不会捕获任何异常。我想从aspx.vb文件访问类文件中抛出的异常。@user1150440-是。在这种情况下,您需要从catch块中重新抛出异常,这样它将冒泡到调用代码。这是我在代码注释和我给你的第一个注释中所做的。重新抛出异常不是一种糟糕的做法吗?重新抛出异常是否有任何负面影响?还有一个疑问……如果我重新抛出异常……这意味着我还必须在aspx.vb页面中有一个try-catch块??那么,有两个try-catch块有什么意义呢(一个在类文件中,一个在aspx.vb文件中)…我宁愿在我的aspx.vb文件中只有一个try-catch块??我可能错了。请解释一下。更新了我的答案,举例说明如何避免捕获,但最终保留并使用,最后使用一个用于整洁的插头。还有一个疑问…如果我重新引用异常…这意味着我也必须在aspx.vb页面中有一个try-catch块??那么有两个try-catch块(一个在类文件中,一个在aspx.vb文件中)有什么意义呢?我宁愿在我的aspx.vb文件中只有一个try-catch块??我可能错了。请解释一下。更新了我的答案,举例说明如何避免捕获,但最终保留并使用,最后使用一个用于整洁的插头。嗯