2

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

4 Answers 4

4

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."

Sign up to request clarification or add additional context in comments.

1 Comment

That's it! Thanks to all of you! wow that was a quick response!
3

Response.Redirect throws a ThreadAbortException to terminate the current request.

Your Catch block is catching that exception.

Comments

2

It may be because of ThreadAbortedException because of Response.Redirect. - MSDN support link

Comments

1

You haven't open the connection. You cannot redirect in an Try/Catch. You should set a boolean variable success to false and check for it after the Try/Catch/Finally and redirect if it's set to false.

Have a look at this SO-Question: Is there something that prevents Response.Redirect to work inside try-catch block?

This would be better:

    Dim success As Boolean = True
    Dim ds As New DataSet
    Using conn As New OleDb.OleDbConnection("Provider=SQLOLEDB;Data Source=(local);Initial Catalog='library';Integrated Security=SSPI;")
        Try
            Dim Sql = "SELECT f1, f2, f3 FROM mydb WHERE ltrim(rtrim(UPPER(username))) = 'MYNAME'"
            Dim cmd = New OleDb.OleDbDataAdapter(Sql, conn)
            conn.Open()
            cmd.Fill(ds)
        Catch ex As Exception
            success = False
        End Try
    End Using
    If Not success Then
        Response.Redirect("goback2.html")
    ElseIf ds.Tables.Count <> 0 AndAlso ds.Tables(0).Rows.Count = 0 Then
        Response.Redirect("goback.html")
    Else
        Response.Redirect("dummy.html")
    End If

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.