andrewmilner
asked on
How to set TimeOut of ObjectDataSource Command - c#
In Visual Studio 2008, C# running with an ObjectDataSource, how can I set the Command TimeOut as I have an SQL query that is taking a long time and causing errors with the default timeout.
I have tried the connection timeout in the sql connection string but he command is still timing out.
I have tried the connection timeout in the sql connection string but he command is still timing out.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
bah hit submit too soon, the command timeout can not be set on the generated dataset adapters because it makes the adapters as private. If you wrote your own ObjectDataSource its even easier, something like the following (sorry for the VB
<DataObjectMethod(DataObjectMethodType.Delete)> _
public bool DeleteEmployee(int EID)
{
SqlConnection conn = New SqlConnection(_connectionString);
SqlCommand cmd = New SqlCommand("my sql syntax", conn)
cmd.CommandTimeout = 20
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = EID
...
}
ASKER
How/where would I enter this partial class for the table adaptor?
I have an xsd file containing the autogenerated tableadaptors and XML behind that.
I have an xsd file containing the autogenerated tableadaptors and XML behind that.
You create a new class file for it. Just add the correct namespace and class name for your table adapter.
ASKER
I'm pulling my hair out with this now. It just won't work.
I've tried the above code suggestion but couldnt get it to work. It compiled fine but just makes no difference.
I now have the code below which is kind of the same anyway.
I have this in a new Class1.cs file with the same namespace etc as my TableAdaptor.
Then i'm calling / setting the timeout from my presentation aspx.cs page. by running the code below on the ObjectDataSource1_ObjectCr eated call.
I know it's calling because I put some response.write in there and it spat it out.
It seems to me that the data is being fetched before the Timeout has been set.
Any ideas?
I can't beleive how much trouble this is causing for something that should be so simple.
I've tried the above code suggestion but couldnt get it to work. It compiled fine but just makes no difference.
I now have the code below which is kind of the same anyway.
I have this in a new Class1.cs file with the same namespace etc as my TableAdaptor.
Then i'm calling / setting the timeout from my presentation aspx.cs page. by running the code below on the ObjectDataSource1_ObjectCr
I know it's calling because I put some response.write in there and it spat it out.
It seems to me that the data is being fetched before the Timeout has been set.
Any ideas?
I can't beleive how much trouble this is causing for something that should be so simple.
//Class1.cs
namespace DataSet1TableAdapters
{
public partial class ReplenReportsTableAdapter
{
public void SetCommandTimeout(int timeout)
{
for (int i = 0; i < this.CommandCollection.Length; i++)
if (this.CommandCollection[i] != null)
this.CommandCollection[i].CommandTimeout = timeout;
}
}
}
//Pres ASPX CS page
protected void ObjectDataSource1_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
if (e.ObjectInstance != null)
{
Response.Write("not null");
DataSet1TableAdapters.ReplenReportsTableAdapter adapter = new DataSet1TableAdapters.ReplenReportsTableAdapter();
adapter.SetCommandTimeout(0);
}
}
ASKER
Dont create a new instance of the table adapter. the arguement of the method contains the instance it will be using
protected void ObjectDataSource1_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
DataSet1TableAdapters.TestTableTableAdapter adpt = (DataSet1TableAdapters.TestTableTableAdapter)e.ObjectInstance;
adpt.CommandTimeout = 0;
}
in C#
Con.timeOut=value;
and for sql refer to this link
https://www.experts-exchange.com/questions/24214585/How-to-properly-use-Try-Catch-with-database-connectivity.html