Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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
0
mainrotor
Asked:
mainrotor
  • 2
1 Solution
 
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
 
jasonduanCommented:
Remove the "Try...Catch..." block from GetOrderInfo() method, keep the one inside cmdFormat_Click().
0
 
RickCommented:
I meant:

If dtt2.Rows.count < 1 Then
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now