views:

1729

answers:

3

Hi there. I'm using v.s 2008 with c#.

I have a .xsd file and it has a table adapter. I want to change table adapter's command time. out.

Thanks for your helps..

+1  A: 

Say your dataset is called MySET.
There is one table called MyTable

MySETTableAdapters.MyTableTableAdapter fAdapter = 
   new MySETTableAdapters.MyTableTableAdapter();
fAdapter.Adapter.SelectCommand.CommandTimeout = <fill inyour value here>;
Julian de Wit
A: 

In some cases you cannot access members like Adapter in your class, since they are defined as private to the class.

Fortunately, the wizard will generate partial classes, which means you can extend them. As described in [this thread by Piebald][1], you can write your own property to set the timeout on select-commands.

Generally, you would do this:

partial class FooTableAdapter
{
  /**
   * <summary>
   * Set timeout in seconds for Select statements.
   * </summary>
   */
  public int SelectCommandTimeout
  {
    set
    {
      for ( int n=0; n < _commandCollection.Length; ++n )
        if ( _commandCollection[n] != null )
          ((System.Data.SqlClient.SqlCommand)_commandCollection[n])
            .commandTimeout = value;
    }
  }
}

Note that I have not actually tried this myself, but it seems like a viable solution.

csl
Thanks! I wasn't able to get this to work for me because _commandCollection was null. I made some updates and posted that as another answer.
Mitchell Gilman
A: 

With some small modifications csl's idea works great.

partial class FooTableAdapter
{
  /**
   * <summary>
   * Set timeout in seconds for Select statements.
   * </summary>
   */
  public int SelectCommandTimeout
  {
    set
    {
            for (int i = 0; i < this.CommandCollection.Length; i++)
                if (this.CommandCollection[i] != null)
                 this.CommandCollection[i].CommandTimeout = value;
    }
  }
}

To use it, just set this.FooTableAdapter.CommandTimeout = 60; somewhere before the this.FooTableAdapter.Fill();


If you need to change the timeout on a lot of table adapters, you could create a generic extension method and have it use reflection to change the timeout.

/// <summary>
/// Set the Select command timeout for a Table Adapter
/// </summary>
public static void TableAdapterCommandTimeout<T>(this T TableAdapter, int CommandTimeout) where T : global::System.ComponentModel.Component
{                
    foreach (var c in typeof(T).GetProperty("CommandCollection", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance).GetValue(TableAdapter, null) as System.Data.SqlClient.SqlCommand[])
        c.CommandTimeout = CommandTimeout;
}

Usage:

this.FooTableAdapter.TableAdapterCommandTimeout(60);
this.FooTableAdapter.Fill(...);

This is a little slower. And there is the possibility of an error if you use it on the wrong type of object. (As far as I know, there is no "TableAdapter" class that you could limit it to.)

Mitchell Gilman