I\'m using Visual Studio 2008 with C#.
I have a .xsd file and it has a table adapter. I want to change the table adapter\'s command timeout.
Thanks for your
This one is a bit old now and suspect this solution is not relevant to everyone, but I've ended up using AniPol's solution to override the ObjectDataSource control as follows:
public class MyObjectDataSource : ObjectDataSource
{
public MyObjectDataSource()
{
this.ObjectCreated += this.MyObjectDataSource_ObjectCreated;
}
private void MyObjectDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
var objectDataSourceView = sender as ObjectDataSourceView;
if (objectDataSourceView != null && objectDataSourceView.TypeName.EndsWith("TableAdapter"))
{
var adapter = e.ObjectInstance;
PropertyInfo adapterProp = adapter.GetType()
.GetProperty(
"CommandCollection",
BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance);
if (adapterProp == null)
{
return;
}
SqlCommand[] commandCollection = adapterProp.GetValue(adapter, null) as SqlCommand[];
if (commandCollection == null)
{
return;
}
foreach (System.Data.SqlClient.SqlCommand cmd in commandCollection)
{
cmd.CommandTimeout = 120;
}
}
}
}
If you go to [name of DataSet].Designer.cs which is a file added under the data set file in the solution and then search for :
private void InitCommandCollection();
This is a function that you should be able to set properties for functions that have been defined in a table adapter.
The first line in that function is
this._commandCollection = new global::System.Data.IDbCommand[<number of function defined in a table adapater>];
and then in the next line for each of those function, you can set
((global::System.Data.SqlClient.SqlCommand)(this._commandCollection[<index>])).CommandTimeout = 0;
which 0 indicates no limitation and the function will not stop due to the time out and also it can be set to 10, 20, 30 or 1000 and so on
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.