Mike
Mike

Reputation: 743

ASP NET Stop other sub proc from executing after error

I am having a problem with my try catch block runs in the error:

database is not available

My problem is the try catch runs and catches the error, but it will not redirect to my error page. The code continues to execute the other subs. I have tried adding: exit, return, response.end. None of them worked. Thank you for your help.

    Imports System.Data
    Imports EUC
    Imports System.Threading
    Imports System.Data.SqlClient

    Partial Class mpMain
        Inherits System.Web.UI.MasterPage

        Dim strSQL As String
        Dim objData As clsDataAccess = New clsDataAccess()
        Dim ds As New D

ataSet
    Dim t1 As DataTable
    Dim intSecurityLevel As String = 0
    Dim strUser As String = UCase(Right(HttpContext.Current.User.Identity.Name.ToString(), 4))

    Protected Sub ExpandNode(ByVal NodeName As String, ByVal PageName As String)

    'More Code

    End Sub


    Protected Sub ExpandNode2(ByVal NodeNameMain As String, ByVal NodeName As String, ByVal PageName As String)

    'More Code

    End Sub


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load


            If Not (IsPostBack) Then

                Try
                    strSQL = "Select SecurityLevel from tblSecurity where SFID = @SFID order by SecurityLevel"

                    Dim MyParameters1 As SqlParameter() = { _
                        New SqlParameter("@SFID", SqlDbType.VarChar) _
                    }

                    MyParameters1(0).Value = UCase(Right(HttpContext.Current.User.Identity.Name.ToString(), 4))

                    ds = objData.SQLExecuteDataset(strSQL, CommandType.Text, MyParameters1)

                    t1 = ds.Tables(0)

                Catch ex As Exception

                    Dim sendError As New clsExceptionMessage
                    sendError.sendMessage(ex.Message, Request.Path, strSQL)
                    Response.Redirect("ErrorMessage.aspx", False)

                End Try

            End If

    End Sub

    Protected Sub TreeView1_TreeNodeDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodeDataBound

    'More Code

    End Sub


    Protected Sub TreeView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.DataBound

    'More Code

    End Sub


End Class

Upvotes: 0

Views: 516

Answers (1)

Justin Wignall
Justin Wignall

Reputation: 3510

Firstly I would catch SQLException, you rarely want to catch a general exception.

In the catch block, set a boolean parameter such as

hasError = True

and once outside the try..catch block check for this to be true and redirect from there.

Upvotes: 2

Related Questions