此功能是在一個類文件中App_Code
文件夾捕獲異常aspx.vb頁
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 [email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],[email protected],Step_Completed='True',Time_Stamp='" & Date.Now & "'", _parameterNames, _parameterVals)
Catch ex As Exception
End Try
現在,我要趕Primary Key Violation
例外,在我的aspx.vb
page ..但我沒有得到在aspx.vb
頁面的例外,因爲錯誤被類函數捕獲。所以,我如何從類文件異常到aspx.vb文件? End Sub
非常感謝您的時間。 – user1150440
只有一個疑問..如果我重新拋出異常...這意味着我將不得不在aspx.vb頁面中有一個try catch塊嗎?所以有兩個嘗試catch塊(一個在課堂上文件和aspx.vb文件中的一個)...而是我的aspx.vb文件只有一個try catch塊?我可能是錯的..簡單地解釋。 – user1150440
更新我的答案與避免捕捉,但保持最後和使用使用替代和最後一個插件Dapper的例子。 HTH – LosManos