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.
andrewmilnerAsked:
Who is Participating?
 
wht1986Commented:
if your object data source is the autogenerated Table adapter, create a partial class of the table adapter and add a public property to set/get the command timeout value
namespace DataSet1TableAdapters
{
    public partial class TestTableTableAdapter
    {
        public int CommandTimeout
        {
            get { return this._commandCollection[0].CommandTimeout; }
            set
            {
                for (int i = 0; i < this._commandCollection.Length; i++)
                    this._commandCollection[i].CommandTimeout = value;
            }
        }
    }
}
 
Then can be called like:
DataSet1TableAdapters.TestTableTableAdapter adpt = new DataSet1TableAdapters.TestTableTableAdapter();
adpt.CommandTimeout = 200;

Open in new window

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

in C#

Con.timeOut=value;

and for sql refer to this link

http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/Q_24214585.html
0
 
wht1986Commented:
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

0
Protect Your Employees from Wi-Fi Threats

As Wi-Fi growth and popularity continues to climb, not everyone understands the risks that come with connecting to public Wi-Fi or even offering Wi-Fi to employees, visitors and guests. Download the resource kit to make sure your safe wherever business takes you!

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

0
 
andrewmilnerAuthor Commented:
0
 
wht1986Commented:
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

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.