juststeve
asked on
Sql Connection error handling
I'm moving a project from ASP Classic to vb.net and need some help transitioning some error trapping code. In the Classic version I had an include page that had a subroutine that included code like:
If objConnection.Errors.count > 0 Then
Dim myError As String = ("The following Connection Errors occured from " & Request.ServerVariables("U RL") & "<BR>")
For iCounter = 0 To objConnection.Errors.count - 1
myError = myError & ("Error Number: " & objConnection.errors(iCoun ter).numbe r & "<br>")
myError = myError & ("Error Description: " & objConnection.errors(iCoun ter).descr iption & "<br>")
myError = myError & ("SQL State: " & objConnection.errors(iCoun ter).SQLSt ate & "<br>")
myError = myError & ("Native Error: " & objConnection.errors(iCoun ter).Nativ eError & "<br>")
Next
Each page would call that sub passing the objConnection = ADODB.Command.ActiveConnec tion. How do I go about coding this for a .net (1.1) project?
thx
--steve...
If objConnection.Errors.count
Dim myError As String = ("The following Connection Errors occured from " & Request.ServerVariables("U
For iCounter = 0 To objConnection.Errors.count
myError = myError & ("Error Number: " & objConnection.errors(iCoun
myError = myError & ("Error Description: " & objConnection.errors(iCoun
myError = myError & ("SQL State: " & objConnection.errors(iCoun
myError = myError & ("Native Error: " & objConnection.errors(iCoun
Next
Each page would call that sub passing the objConnection = ADODB.Command.ActiveConnec
thx
--steve...
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Makes sense...this followup is a bit off topic of SqlConnection handling toward the Err. object...
In the Classic version i just called the handler at the bottom of the page and checked: 'If Err.Number <> 0 Then '...Am I expected to put that at the bottom of each of my Subs now? Or where can I put it to trap any error just prior to rendering?
In the Classic version i just called the handler at the bottom of the page and checked: 'If Err.Number <> 0 Then '...Am I expected to put that at the bottom of each of my Subs now? Or where can I put it to trap any error just prior to rendering?
Where ever u have sqlconnection to connect to database, put the SqlConnection command inside the TRY CATCH Block like below
public sub Getconnection ()
Try
....
....sqlconnection.Open() ' if sqlconneciton throws error then it goes to catch block
Catch err as SqlException
DisplaySqlErrors(err) ' Else u can put all the code here itself, which is therein my previous msg
End sub
-Aaron
public sub Getconnection ()
Try
....
....sqlconnection.Open() ' if sqlconneciton throws error then it goes to catch block
Catch err as SqlException
DisplaySqlErrors(err) ' Else u can put all the code here itself, which is therein my previous msg
End sub
-Aaron
Public Sub DisplaySqlErrors(myExcepti
Dim i As Integer
For i = 0 To myException.Errors.Count - 1
MessageBox.Show("Index #" & i & ControlChars.NewLine & _
"Error: " & myException.Errors(i).ToSt
Dim myError As String = ("The following Connection Errors occured from " & Request.ServerVariables("U
For iCounter = 0 To objConnection.Errors.count
myError = myError & ("Error Number: " & myException.Errors(i).Numb
myError = myError & ("Error Description: " & myException.Errors(i).Mess
myError = myError & ("SQL State: " & myException.Errors(i).Stat
Next i
End Sub
-Aaron