1

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.

9
  • Increase the value of Connection Timeout in your connection string. It should be in config file. Commented Sep 7, 2018 at 20:37
  • Code sets the stored procedure in SelectCommand property, and then calls SqlDataSource.Select(). Commented Sep 7, 2018 at 20:39
  • @user1080381 Sorry I misread your suggestion, I will try that. Commented Sep 7, 2018 at 20:40
  • Some code on the database call with it's parameters and command would be helpful. Commented Sep 7, 2018 at 20:42
  • Why are you creating a SqlDataSource directly in code? Why aren't you using a SqlConnection and SqlCommand? To me, that's the far more pressing matter - a SqlDataSource is meant to be declared in ASPX markup and used there. It doesn't belong directly in C#. I know from experience. I used to use SqlDataSource everywhere and it was a giant pain. Commented Sep 7, 2018 at 20:44

1 Answer 1

1

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());
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.