Link to home
Start Free TrialLog in
Avatar of andrewmilner
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.
Avatar of amar31282
amar31282
Flag of India image

you have to change timeout in SQL server and in C# part.

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
ASKER CERTIFIED SOLUTION
Avatar of wht1986
wht1986
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
...
}

Open in new window

Avatar of andrewmilner
andrewmilner

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.
You create a new class file for it. Just add the correct namespace and class name for your table adapter.
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_ObjectCreated 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.


//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);
        }
    }

Open in new window

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;
    }

Open in new window