I have a gridview with a DataSourceID set, so the databinding happens automatically. The problem is that sometimes, the procedure defined in the SqlDataSource takes a very long time to finish, so the binding comes with a timeout expired error.

How can I catch this error without manually databinding the gridview and surrounding it with try/catch statements?

4 Answers
4

If an exception occurs when the SqlDataSource is executing, it fires its appropriate post-action event - Selected, in this case. You can optionally create an event handler for this event and in the event handler say that you've handled the exception.

This diagram shows how this interaction works with the ObjectDataSource (the concept is the same with the SqlDataSource control). When examining the diagram below, replace the words "ObjectDataSource" with "SqlDataSource" and "Underlying Object" with "Database" to have it be pertinent for the SqlDataSource.

As you can see, the Selecting event is raised before the data is sent off to the database and the Selected event is raised after the data comes back (or if there's an exception).

The problem is we have several procedures that work fine on the development server, but take a very long time to load on the production one (different SQL Server versions). So, until we get to identify and fix all of them, we'd like to replace the timeout error with something a bit more user friendly :)
–
AnonymousJun 19 '09 at 15:29

In that case, use DataSource and DataBind.
–
RichardODJun 19 '09 at 15:40

There is no way to catch the exceptions caused by timeouts used in the Sql DataSource control- hence the "old school" way of DataSource and DataBind will work better in this scenario. Here's a code example- msdn.microsoft.com/en-us/library/fkx0cy6d.aspx
–
RichardODJun 19 '09 at 15:45

@Scott Mitchell- that is true. Of course my preference would always be to investigate why the query is timing out in the first place. Completely off topic but I remember reading your Designing Active Server Pages book many years ago!
–
RichardODAug 30 '10 at 19:23