I need help with Error Trapping and Handling in ASP.Net 3.5

Hi Experts,
I need help handling server side errors in my ASP.Net 3.5 application.  Below I have included my code.  I call various procedures and functions from a button's click event.  If one of the procedures fails, how can i make sure that the error is trapped correctly and the rest of my functions and procedures don't execute?  What's the correct way of handling these type of errors in ASP.Net?


Protected Sub cmdFormat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdFormat.Click
      Try

      Call GetOrderInfo()
        Call GenerateOrders()

      Catch ex As Exception
         lblError.Text = "Error code: " & ex.ErrorCode & " - " & "Error message: " & ex.Message.ToString
      End Try
End Sub


Protected Sub GetOrderInfo()
        Dim dtt2 As New DataTable
        Try
            Using cn As SqlConnection = GetMyConnection()
                Dim cm As New SqlCommand("dbo.GetOrderInfo", cn)
                cm.CommandType = CommandType.StoredProcedure
                cm.Parameters.Add("@pi_intDefaultID", SqlDbType.Int).Value = 1
                cn.Open()
                dtt2.Load(cm.ExecuteReader())
            End Using

            dtt2.DefaultView.RowFilter = "OrderID = " & ddlOrder.SelectedValue
            If dtt2.DefaultView.Count = 1 Then
                strOrderNo = dtt2.DefaultView.Item(0).Item("OrderNo")
                strDesc = dtt2.DefaultView.Item(0).Item("Description")
            End If
        Catch ex As SqlException
            lblError.Text = "Error code: " & ex.ErrorCode & " - " & "Error message: " & ex.Message.ToString
        End Try
End Sub


Thanks in advance,
mrotor
mainrotorAsked:
Who is Participating?
 
jasonduanCommented:
Remove the "Try...Catch..." block from GetOrderInfo() method, keep the one inside cmdFormat_Click().
0
 
RickCommented:
You could move dtt2 from local to global declaration, then:
Protected Sub cmdFormat_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdFormat.Click
      Try

      Call GetOrderInfo()

       If dtt2.DefaultView.Count <> 1 Then
        'Display your error message
        Exit Sub
       Else
        Call GenerateOrders()
       End If
       

      Catch ex As Exception
         lblError.Text = "Error code: " & ex.ErrorCode & " - " & "Error message: " & ex.Message.ToString
      End Try
End Sub


Protected Sub GetOrderInfo()
       dtt2 = New DataTable
        Try
            Using cn As SqlConnection = GetMyConnection()
                Dim cm As New SqlCommand("dbo.GetOrderInfo", cn)
                cm.CommandType = CommandType.StoredProcedure
                cm.Parameters.Add("@pi_intDefaultID", SqlDbType.Int).Value = 1
                cn.Open()
                dtt2.Load(cm.ExecuteReader())
            End Using

            dtt2.DefaultView.RowFilter = "OrderID = " & ddlOrder.SelectedValue
            If dtt2.DefaultView.Count = 1 Then
                strOrderNo = dtt2.DefaultView.Item(0).Item("OrderNo")
                strDesc = dtt2.DefaultView.Item(0).Item("Description")
            End If
        Catch ex As SqlException
            lblError.Text = "Error code: " & ex.ErrorCode & " - " & "Error message: " & ex.Message.ToString
        End Try
End Sub

Open in new window

0
 
RickCommented:
I meant:

If dtt2.Rows.count < 1 Then
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.