Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net vb codebehind try ... catch issue

In codebehind I have the following:

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
    Dim sql As String
    Dim conn As OleDbConnection
    Dim cmd As OleDbDataAdapter
    Dim ds As DataSet
    Dim tbl As DataTable

    conn = New OleDbConnection("Provider=SQLOLEDB;Data Source=(local);Initial Catalog='library';Integrated Security=SSPI;")
    Try
        sql = "SELECT f1, f2, f3 FROM mydb WHERE ltrim(rtrim(UPPER(username))) = 'MYNAME'"

        cmd = New OleDbDataAdapter(sql, conn)
        ds = New DataSet
        cmd.Fill(ds)
        tbl = New DataTable
        tbl = ds.Tables(0)

        If tbl.Rows.Count = 0 Then
            Response.Redirect("goback.html")
        End If

        Response.Redirect("dummy.html")

    Catch ex As Exception
        Response.Redirect("goback2.html")
    End Try

End Sub

The routine works to a point. I can check the value of tbl.Rows.Count = 1, so it should redirect to "dummy.html" WHICH IT DOES CORRECTLY so long as I comment out the line that redirects to "goback2.html".

If I uncomment the line to redirect to goback2, then it goes to "goback2.html"

I thought it should only execute that code if there was some error in the previous code - but there can't be an error in the previous code, if it executes. It's like it's executing the catch code regardless of what I'm going.

Oddly, it ONLY messes up with the redirect! If I replace the redirect to goback2 with an assignment to a textbox.text then it works (by ignoring that code) - but the redirect it seems to execute regardless of whether it should take the catch

like image 318
elbillaf Avatar asked Aug 30 '26 02:08

elbillaf


1 Answers

Response.Redirect(string) throws a ThreadAbortException when Response.End() is called.

Use the overload that takes a string and a boolean instead:

Response.Redirect("goback.html", false);

From MSDN: the second parameter, endResponse, "Indicates whether execution of the current page should terminate."

like image 141
Town Avatar answered Aug 31 '26 18:08

Town



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!