How do I prevent a timeout error when executing a store procedure using a SqlCommand?

The timeout on the connection is for connecting to the database only.

There is a separate CommandTimeout property of the SqlCommand class, use this property to specify the execution timeout.

Ie.

using (SqlCommand cmd = new SqlCommand())
{
  cmd.Connection = connection1;
  cmd.CommandTimeout = 240; //in seconds
  //etc...
}

Use SqlCommand.CommandTimeout property of your command instead of specifying it in connection string.

See MSDN for reference.


You need to set it in code i.e by setting the CommandTimeout property of the sql command object.

The parameter 'connection timeout' in connection string represents the time to wait while trying to establish a connection before terminating the attempt and generating an error. It's not the time after which query execution will time out.

Thanks, I have also faced the same issue some weeks ago and was confused between the time out values in webconfig vs in command object. Your question cleared has my doubt now :)

reference link from msdn