Powered By Blogger

Sunday, December 12, 2010

Increase Default Command timeout

During my last project I encountered this kind of error “An error has occurred during report processing. Exception has been thrown by the target of an invocation. Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding
My first suspect was that it has something to do with my connectionstring timeout. But after increasing the timeout the error still exist. To complicate things, if the dataset contains only a few data the error would disappear. This comes to the conclusion that it has something to do with command timeout. As command timeout and connection timeout is totally different entity. The command timeout already lapsed and that the data has not yet been fully rendered to the page. Thus, leading to the error mentioned above.
The challenge now is to increase the command timeout of the datasetAdapter since the object  is being generated by the wizard. However, we can create a partial class that extends the datasetAdapter created by the wizard for us. The key really is to have the right namespace same of that namespace created by the wizard. We can easily look the right namespace by switching from solution explorer to classview. After locating the correct namespace, we are now ready to create a partial class that extend the command timeout of the command collection. Below is the code. Note that the xxx is correct namespace of your .datasetAdapter and the yyy is the name of your tableAdapter. Failing to locate the correct namespace we will not be able to increase the default of 30 seconds command timeout. And that the compiler will not be able to locate the this.CommandCollection object.
Below is the partial class that of the adapter we’ve created.
namespace xxxDataSetTableAdapters
{
    public partial class yyyAdapter
    {
        public int CommandTimeout
        {
            set
            {
               int i =0;
                for (int i = 0; (i < this.CommandCollection.Length); i = (i + 1))
                {
                    if ((this.CommandCollection[i] != null))
                    {
                        this.CommandCollection[i].CommandTimeout = value;
                    }
                }
            }
        }
    }
}
After creating the partial class, all we need now is to instantiate the partial class we’ve created on the on the aspx page.
using xxxDataSetTableAdapters;

protected void MyDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
        yyyAdapter adapter = (yyyAdapter).e.ObjectInstance;
        adapter.CommandTimeOut= 180;
}

I’m documenting what I did hoping that it would also be of help to other developers out there. Please note thought that this is just a stop gap. The best practice would still be to create a DAL that will hold the dataset since relying much on wizard sometimes will lead us to dig further and that scalability would be an issue later on since we don’t have the full grasp of the entire code and processes.
Happy coding..