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("URL") & "<BR>")

            For iCounter = 0 To objConnection.Errors.count - 1

                myError = myError & ("Error Number: " & objConnection.errors(iCounter).number & "<br>")
                myError = myError & ("Error Description: " & objConnection.errors(iCounter).description & "<br>")
                myError = myError & ("SQL State: " & objConnection.errors(iCounter).SQLState & "<br>")
                myError = myError & ("Native Error: " & objConnection.errors(iCounter).NativeError & "<br>")
            Next

Each page would call that sub passing the objConnection = ADODB.Command.ActiveConnection. How do I go about coding this for a .net (1.1) project?

thx
--steve...
juststeveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

existenz2Commented:
This is a C# example, but you can easily rewrite it for VB.NET:

try
{
                        
}catch(SqlException ex)
{
    foreach(SqlError error in ex.Errors)
    {
            Debug.WriteLine(error.Number);
            Debug.WriteLine(error.Message);
            Debug.WriteLine(error.State);
            Debug.WriteLine(error.Class);
    }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aaron JabamaniTechnical ArchitectCommented:
Hope below helps

Public Sub DisplaySqlErrors(myException As SqlException)
    Dim i As Integer
    For i = 0 To myException.Errors.Count - 1
        MessageBox.Show("Index #" & i & ControlChars.NewLine & _
                        "Error: " & myException.Errors(i).ToString() & ControlChars.NewLine)
        Dim myError As String = ("The following Connection Errors occured from " & Request.ServerVariables("URL") & "<BR>")

            For iCounter = 0 To objConnection.Errors.count - 1

                myError = myError & ("Error Number: " & myException.Errors(i).Number   & "<br>")
                myError = myError & ("Error Description: " & myException.Errors(i).Message  & "<br>")
                myError = myError & ("SQL State: " & myException.Errors(i).State  & "<br>")
     Next i
End Sub

-Aaron
juststeveAuthor Commented:
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?
Aaron JabamaniTechnical ArchitectCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.