Setting TimeOut on Typed DataSet TableAdapter

Share

If you have a SQL query you’ve added to a TableAdapter that takes longer than 30 seconds to run, you’ll get a timeout error. You’ll also find that there’s no timeout property to set, so what do you do?

Fortunately, in .NET 2.0, you have partial classes. I won’t give a tutorial of partial classes in this article, but in short, it’s a way for you to add to an existing class (even one that’s already compiled), without having to inherit a new class from it. So, using a partial class, we can expose the protected timeout properties that we normally wouldn’t have access to. The code image below shows how to do that. Just type this anywhere in your project, replacing my namespaces and class names with yours. If you’re using .NET 1.0 or 1.1, you’ll have to inherit a new class from the one you want to set timeouts on. In your new class, you’ll add similar code below, but you don’t have TableAdapter’s in .NET 1.0 or 1.1. This post is specifically about .NET 2.0, but with a little work, you should be able to adapt it to 1.0 or 1.1. The key is inheritance.


namespace MyNameSpace.MyDataSetTypeTableAdapters
{
public partial class MyTableAdapter
{
public int InsertCommandTimeout
{
get { return this.Adapter.InsertCommand.CommandTimeout; }
set { this.Adapter.InsertCommand.CommandTimeout = value; }
}
public int UpdateCommandTimeout
{
get { return this.Adapter.UpdateCommand.CommandTimeout; }
set { this.Adapter.UpdateCommand.CommandTimeout = value; }
}
public int DeleteCommandTimeout
{
get { return this.Adapter.DeleteCommand.CommandTimeout; }
set { this.Adapter.DeleteCommand.CommandTimeout = value; }
}
public int SelectCommandTimeout
{
get { return this.CommandCollection[0].CommandTimeout; }
set
{
for (int x = 0; x < this.CommandCollection.Length; x++)
if (this.CommandCollection[x] != null)
((System.Data.SqlClient.SqlCommand)this.CommandCollection[x]).CommandTimeout = value;
}
}
}
}
Share