ASP.Net VB Redirecting to an error page on SQLDataSource timeout


My page has the following SQLDataSource

<asp:SqlDataSource ID="dsResults" runat="server"
    ConnectionString="<%$ ConnectionStrings:Activist_InsightConnectionString %>"
    EnableViewState="False" >

At the moment I am setting the timeout to be very low to force at timeout, ultimatly this number will be higher.

Protected Sub dsResults_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles dsResults.Selecting

    e.Command.CommandTimeout = 12

  End Sub

I want a friendly error page if the Query times out, so I tried the following:

      dsResults.SelectCommandType = SqlDataSourceCommandType.Text
      dsResults.SelectCommand = BuildMasterQuery()
    Catch sqlEx As SqlException
      Response.Redirect("SearchTimeout.aspx?queryid=" & Request.QueryString("queryid"))
    Catch Ex As Exception
      Response.Redirect("SearchTimeout.aspx?queryid=" & Request.QueryString("queryid"))
    End Try

However when the query times out I do not redirect to the speciifed page, but I get an error page.

What am I doing wrong?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

This type of error has to be handled by the application error handler.
The event must be created in your global.ascx
Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
    Session("CurrentError") = "Global: " & _
End Sub

Open in new window

rpmAuthor Commented:
Ok this approach looks promising.

How do I tell if the error has come from the page that I am interested in?

in the application error event use something like this

Dim con As HttpContext = HttpContext.Current
con.Request.Url.ToString() 'will contain the page throwing the exception

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
rpmAuthor Commented:
That's done the trick, thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.