2012-07-01 20 views
0

此功能是在一個類文件中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

回答

2

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

Try 
    .... 
Catch ex As Exception 
    do some stuff with ex 
    Throw ' important to not use ex as parameter as that would start a new exception stack. 
Finally 
    do some other stuff 
End Try 

既然你沒有做任何事情在catch只是跳過它。

Try 
    .... 
Finally 
    do some other stuff 
End Try 

爲了使代碼更好地閱讀Using。通過使用,你可以讓編譯器爲你隱式插入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 。 Dapper是Stack overflow運行的東西。這是一個很好的小型庫,使編寫sql調用更容易編寫和讀取。

+0

非常感謝您的時間。 – user1150440

+0

只有一個疑問..如果我重新拋出異常...這意味着我將不得不在aspx.vb頁面中有一個try catch塊嗎?所以有兩個嘗試catch塊(一個在課堂上文件和aspx.vb文件中的一個)...而是我的aspx.vb文件只有一個try catch塊?我可能是錯的..簡單地解釋。 – user1150440

+0

更新我的答案與避免捕捉,但保持最後和使用使用替代和最後一個插件Dapper的例子。 HTH – LosManos

1

BCL沒有可以被捕獲的PrimaryKeyViolationException

最接近的是要抓住SqlException並查看消息以查看SQL Server報告的問題。該Catch塊應爲以上捕獲塊爲Exception,因爲它更具體。

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 
+0

如何從aspx.vb文件中獲取異常?如果我在類文件中有一個try catch塊,它會捕獲異常..所以如果我在aspx.vb文件中使用try catch塊...它不會捕獲任何異常。 – user1150440

+0

@ user1150440 - 重新拋出異常。 http://support.microsoft.com/kb/315965 – Oded

+0

我想從aspx.vb文件訪問類文件中引發的異常。 – user1150440