Search code examples
c#tableadapter

tableadapters issue with SelectCommandTimeout property c#


I want to increase the time to retrieve data from my tableadapter. How do i set it? I tried using this:

http://www.codeproject.com/KB/database/TableAdaptrCommandTimeout.aspx

However, the _commandCollection.Length is set to null therefore i am unable to set the CommandTimeout

Any ideas?


Solution

  • You have to call the GetData() Method on your tableAdapter before you can set the timeout, othewise the SelectCommand will not have been initialized.

    protected void setAdapterTimeout(SqlDataAdapter da, int timeOut = 120)
        {
    
            if (da.SelectCommand != null)
                da.SelectCommand.CommandTimeout = timeOut;
    
        }
    

    Then call it like this:

     //Replacing AccessoryTableAdapter with your table Adapter
     AccessoryTableAdapter ata = new AccessoryTableAdapter();
     setAdapterTimeout(ata.Adapter);
    

    EDIT: Extension Methods are cool!

    public static class Extensions
    {
        public static void setAdapterTimeout(this SqlDataAdapter da, int timeOut = 120)
        {
            if (da.SelectCommand != null)
                da.SelectCommand.CommandTimeout = timeOut;
            if (da.InsertCommand != null)
                da.InsertCommand.CommandTimeout = timeOut;
    
        }
    }
    

    then call it:

     AccessoryTableAdapter ata = new AccessoryTableAdapter();
     ata.Adapter.setAdapterTimeout(120);