How to set commandTimeout for ObjectDataSource (ASP.NET 2.0)?

P

Piotrek

HI all.

In my web site I have some ObjetDataSource. Its business object
property is set to my dataset I created in dataset designer (this
dataset has table adapter).

My question is: how can I set command timeout property for fill method
from this table adapter?

Thanks in advance,
Piotrek.
 
J

jd

The timeout is normally set in the connection string. I don't know if
there's another place to specify it. If you want to use the same
timeout everywhere in your program, then just set the timeout value
globally. If you want to use different timeouts in different places,
you can dynamically build the connection string (note, however, that
this may reduce the effectiveness of connection pooling).

Hope this helps...

-- jeff
 
P

Piotrek

Thanks jd.

However, I don't want to set Connection timeout but Command timeout. In
connection string only Connection timeout can be set.

Piotrek
 
P

Piotrek

Jd, that's exaclty what I want to do.

Just tell me, where I can find this CommandTimeout property (remember,
that I use auto generated table adapters).

Piotrek
 
K

Kevin Spencer

It's fine to use autogenerated code, but at some point you need to tweak it.
In this case, you have code that creates a TableAdapter. A TableAdapter is a
wrapper for a DataAdapter. The DataAdapter in the Table Adapter has 4
different Command objects associated with it, one each for inserting,
selecting, updating, and deleting. The Command object has the CommandTimeout
property which can be used to adjust this.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
P

Piotrek

Thanks for all answers.

I created a partial class, in which there is a SelectCommandTimeout
property:
namespace DataSetPaymentsTableAdapters
{
public partial class P_RWI_PAYMENTS_SEARCHTableAdapter
{
public int SelectCommandTimeout
{
get
{
return (this._commandCollection[0].CommandTimeout);
}


set
{
for (int i = 0; i < this._commandCollection.Length;
i++)
{
if ((this._commandCollection != null))
{

((System.Data.SqlClient.SqlCommand)(this._commandCollection)).CommandTimeout
= value;
}
}
}
}


}
}

My solution compiles without errors, but I do not how to call this
newly created property from the page, on which I have my
ObjectDataSource.

I would like to do sth like that (e.g. in the Page_Load event):
P_RWI_PAYMENTS_SEARCHTableAdapter.SelectCommandTimeout = 180
but this TableAdapter is not seen in IntelliSense.

Piotrek
 
J

Juan T. Llibre

re:
I would like to do sth like that (e.g. in the Page_Load event):
P_RWI_PAYMENTS_SEARCHTableAdapter.SelectCommandTimeout = 180
but this TableAdapter is not seen in IntelliSense.

It's simpler to set the Timeout in code :

<DataObjectMethod(DataObjectMethodType.Delete)> _
Public Shared Function DeleteEmployee(EID As Integer) As Boolean

If Not _initialized Then Initialize()

Dim conn As SqlConnection = New SqlConnection(_connectionString)
Dim cmd As SqlCommand = New SqlCommand("DELETE FROM E WHERE EID = @EmployeeID", conn)
cmd.CommandTimeout = 20
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = EID

Try
conn.Open()

If cmd.ExecuteNonQuery() <> 0 Then _
Return False
Catch e As SqlException
' Handle exception.
Finally
conn.Close()
End Try

Return True
End Function




Piotrek said:
Thanks for all answers.

I created a partial class, in which there is a SelectCommandTimeout
property:
namespace DataSetPaymentsTableAdapters
{
public partial class P_RWI_PAYMENTS_SEARCHTableAdapter
{
public int SelectCommandTimeout
{
get
{
return (this._commandCollection[0].CommandTimeout);
}


set
{
for (int i = 0; i < this._commandCollection.Length;
i++)
{
if ((this._commandCollection != null))
{

((System.Data.SqlClient.SqlCommand)(this._commandCollection)).CommandTimeout
= value;
}
}
}
}


}
}

My solution compiles without errors, but I do not how to call this
newly created property from the page, on which I have my
ObjectDataSource.

I would like to do sth like that (e.g. in the Page_Load event):
P_RWI_PAYMENTS_SEARCHTableAdapter.SelectCommandTimeout = 180
but this TableAdapter is not seen in IntelliSense.

Piotrek
 
P

Piotrek

Hi Juan.
It's simpler to set the Timeout in code :
I know that, however as I said I did not write any line of code -
everything was autogenerated by DataSet Designer. It would be great if
I could change this generated code by setting there timeout.
Unfortunately I cannot find this code. Morover I think that every time
I change something in my dataset then this code would be generated one
more time and my changes would be overridden.

Piotrek
 
U

usenetlex

Hey, did you ever figure this out?
I am having the same problem - in the DataSet desginer, I do not see a
way to set the CommandTimeout (just the CommandText, CommandType,
etc.).
Changing the code... well I would lose it on the next auto-generate.
And the command variables are private.
ARGH.
 
P

Piotrek

No I didn't. I just wrote my own DAL class instead of that one, which
was generated by DataSet Designer.

Piotrek
 
Joined
Aug 3, 2007
Messages
1
Reaction score
0
RE: How to set commandTimeout for ObjectDataSource

Piotrek said:
HI all.

In my web site I have some ObjetDataSource. Its business object
property is set to my dataset I created in dataset designer (this
dataset has table adapter).

My question is: how can I set command time-out property for fill method
from this table adapter?

Thanks in advance,
Piotrek.

Here is your answer:

1. Open your Dataset (lets say MyDataSet.xsd file).
2. Double click on your table adapter (lets say MyTableAdapter)
Visual Studio will switch you to MyDataSet.cs file and will generate similar code for you:

Code:
namespace Company.Project.DataProvider {
    partial class MyDataSet
    {
        partial class MyDataTable
        {
        }
    }
}

3. Now you need to add new property which will help to set command time-out for all commands controller by MyTableAdapter:

Code:
[COLOR="Silver"]namespace Company.Project.DataProvider {
    partial class MyDataSet
    {
        partial class MyDataTable
        {
        }
    }
}[/COLOR]

namespace Company.Project.DataProvider.MyDataSetTableAdapters
{
    public partial class MyTableAdapter
    {
        public int CommandTimeout
        {
            set
            {
                for (int i = 0; (i < this.CommandCollection.Length); i = (i + 1))
                {
                    if ((this.CommandCollection[i] != null))
                    {
                        this.CommandCollection[i].CommandTimeout = value;
                    }
                }
            }
        }
    }
}

4. Open ASPX page where you are using ObjetDataSource (lets say MyObjetDataSource
5. Select MyObjetDataSource, click with right-mouse on it and from menu choose Properties
6. Visual studio will open Properties window for MyObjetDataSource
On top of all properties list there is toolbar. Click lightning icon to see all events of MyObjetDataSource.
7. Double click on ObjectCreated event
8. Visual Studio will open source code of ASPX page and will generate event function for you:

Code:
protected void MyDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
}

9. Create code which will set Command Timeout just after MyDataSource has been created created

Code:
[COLOR="Silver"]protected void MyDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{[/COLOR]
    Company.Project.DataProvider.MyDataSetTableAdapters.MyTableAdapter adapter;
    adapter = (Company.Project.DataProvider.MyDataSetTableAdapters.MyTableAdapter)e.ObjectInstance;
    // Set command timeout to 2 minutes
    adapter.CommandTimeout = 120;
[COLOR="Silver"]}[/COLOR]

Done. :)

Keywords: C#, commandTimeout, ObjectDataSource, DataSet, TableAdapter, Designer, Solution, Code Example, Resolved, www.jocys.com
 
Last edited:
Joined
Apr 16, 2008
Messages
1
Reaction score
0
Hi EJocys,
That approach does not work for me.

The "code behind" for my XSD file is XML. In other words, I get as far as this step:
1. Open your Dataset (lets say MyDataSet.xsd file).
2. Double click on your table adapter (lets say MyTableAdapter)

Double-clicking my table adapter does nothing. When I right-click and select "View Code" I see only XML.

Anyone know what gives?
 
Joined
Aug 20, 2008
Messages
1
Reaction score
0
I made it work.
I had the same issue like you ... code view only showing XML junk.

Added a new PARTIAL class with namespace matching my TableAdapter.
Something like
Code:
namespace [COLOR="Red"]CAATTableAdapters [/COLOR]
    {
    public partial class [COLOR="#ff0000"]SomeTableAdapterInsideCAATTableAdapter[/COLOR]
        {
            public int CommandTimeout
            {
                set
                {
                    for (int i = 0; (i < this.CommandCollection.Length); i = (i + 1))
                    {
                        if ((this.CommandCollection[i] != null))
                        {
                            this.CommandCollection[i].CommandTimeout = value;
                        }
                    }
                }
            }
        }
    }

Then added following to my ObjectDataSource event
CAATDataSource_ObjectCreated

Code:
Dim adapter As New CAATTableAdapters.SomeTableAdapterInsideCAATTableAdapter
        adapter = CType(e.ObjectInstance, CAATTableAdapters.SomeTableAdapterInsideCAATTableAdapter)
        adapter.CommandTimeout = 300

And yes I mixed VB and C# .. it's all same to me.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top