views:

705

answers:

4

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?

A: 

Why not fix the problem with the query timing out instead? Either optimise the DB (preferred) or set the connection/command timeout to be higher than the current value.

You can adjust the timeout as follows by hooking into the SqlDataSource Selecting event:

protected void ds_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        e.Command.CommandTimeout = 5000;
    }

If you are using SQL Server you might want to look at tools like the index tuning wizard/tuning advisor, show query execution plan or SQL Server Profiler.

RichardOD
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 :)
In that case, use DataSource and DataBind.
RichardOD
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- http://msdn.microsoft.com/en-us/library/fkx0cy6d.aspx
RichardOD
Increasing the command timeout fixes the symptoms, not the problem. :-)
Scott Mitchell
@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!
RichardOD
A: 

how about binding asynchronously? once completed, the callback function can call databind if no errors were returned.

EDIT: I guess that's manual...not what you wanted.

A: 

The only think you can do is handling the Page_Error Event

http://msdn.microsoft.com/en-us/library/ed577840.aspx

A: 

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.

The datasource control raises events before and after its action.

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

You can create a Selected event handler in your page and check to see if an exception occurred and decide whether you want to handle it yourself. Fredrik Normen has a good blog entry on this: Handle the data-source control exception by your own.

Additional reading material: Accessing and Updating Data in ASP.NET: Examining the Data Source Control's Events.

Happy Programming!

Scott Mitchell