views:

136

answers:

2

I am using ASP.NET 2.0.

When i place a TRY CATCH block in my event it always go into the CATCH section, in my case it re-direct the page to Default.aspx. But if I remove the TRY CATCH block the code get's executed fine and it does what it suppose to.

Protected Sub gridResults1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridResults1.SelectedIndexChanged

    Try

        Dim selectedRowIndex As Integer
        selectedRowIndex = gridResults1.SelectedIndex

        Dim row As GridViewRow = gridResults1.Rows(selectedRowIndex)
        Dim theCompanyProfile As String = gridResults1.DataKeys(selectedRowIndex).Value

        Response.Redirect("Report.aspx?ID=" + theCompanyProfile)

    Catch ex As Exception

        Response.Redirect("Default.aspx")

    End Try

End Sub
  • There is no error message when i place a break point by the "Catch ex As Exception"
  • Am I reading the selectedRowIndex DataKey value incorrectly maybe?

Thanks in advanced!

A: 

Just to be sure, are you setting the DataKeyNames property on the GridView?

Chris Haas
That is correct Yes!
Etienne
+1  A: 

This is because the Response.Redirect method terminates the execution thread of the page and throws an System.Threading.ThreadAbortException.

You can catch the ThreadAbortException and ignore it (not a best practice, I guess), or pass false for the second parameter (endResponse) to the Response.Redirect method.

Check out this page for more info: http://support.microsoft.com/kb/312629

alexphi