Search code examples
c#sqldatasource

Timeout for SqlDataSource


I'm keeping alive a C# app in VS 2010 that I inherited, and I'm stuck with a timeout issue.

A stored procedure is being called that is now taking much longer to execute is generating the following error:

Could not retrieve record data: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

The SqlDataSource used is instantiated directly in code, and I don't know how to set the CommandTimeout property as it doesn't seem available. Is this true? If not how can I access that property?

The solutions I've seen have the SqlDataSource in the .aspx file and typically CommandTimeout is set in a control (ex: gridview) event. That's not the case here.

EDIT:

Some code

Results = new SqlDataSource();

Results.ConnectionString = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["EM"].ConnectionString;

Results.SelectCommandType = SqlDataSourceCommandType.Text;

Results.SelectCommand = "exec sproc";

DataView ReturnedDataSet = (DataView)Results.Select(new 
System.Web.UI.DataSourceSelectArguments());

Thanks.


Solution

  • Although I strongly advise you to move (run!) away from SqlDataSource, you can set the command timeout from C#. There's an event that you can wire up an event handler to, this exposes the DbCommand where you can set the timeout.

    var ds = new SqlDataSource("connectionString", "select * from dbo.Project_Master");
    ds.Selecting += (object s, SqlDataSourceSelectingEventArgs e) => e.Command.CommandTimeout = 1000;
    var dataView = (DataView)ds.Select(new DataSourceSelectArguments());