Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asp.net c# SqlDataSource timout problems

Tags:

c#

asp.net

I'm trying to extend the timeout of an SqlDataSource beyond 30 second (Seems to be the default). I'm trying to run a stored procedure that has to run through 100,000s of records. During busy periods it times out. I'm using ASP.NET 4.0 and IIS 6.0 on 2003 server.

Error Message: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.

I've tried to no avail to extend the timeout:

< asp:SqlDataSource ID="dsTest" EnableCaching="true" CacheDuration="604800" runat="server" ConnectionString="<%$ ConnectionStrings:SuperNARIC %>" SelectCommand="selectStatus" SelectCommandType="StoredProcedure" onselecting="dsTest_Selecting" >
    <SelectParameters>
        < asp:ControlParameter ControlID="ddlCar" Name="CountryID" PropertyName="SelectedValue" Type="Int32" />
    < /SelectParameters>
< /asp:SqlDataSource>



protected void dsTest_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {
        e.Command.CommandTimeout = 300;
    }

Any help would be greatly appreciated.

Thanks

like image 849
user1024416 Avatar asked Apr 23 '12 19:04

user1024416


2 Answers

Like mentioned here, this worked for me.

You can increase the Timeout property like this

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
        {
            e.Command.CommandTimeout = 0;
        }

Setting timeout to 0 means no timeout

like image 93
Harry Avatar answered Oct 05 '22 04:10

Harry


There are two types of timeout: ConnectionTimeout and CommandTimeout:

ConnectionTimeout Determines the maximum time your app will wait to establish a connection to your server.

CommandTimeout: Max time allowed for a command to execute.

Make sure you set both. In the Comand:

 command.CommandTimeout = 300;

Note: This can be implemented in the Selecting event should your command be part of a DataSource. e.Command.CommandTimeout = 0; A value of 0 means to wait indefinitely.

And connection string:

SqlConnectionStringBuilder cs = new SqlConnectionStringBuilder(connectionString);
cs.ConnectTimeout = 300;

Or:

<add name="MyConnectionString" connectionString="Data Source=.\SQLEXPRESS; Database=MyDB; Integrated Security=True;Pooling=True;connection timeout=30" providerName="System.Data.SqlClient" />

Note: Try setting the connection string timeout globally, perhaps in your config file.

like image 31
Ulises Avatar answered Oct 05 '22 04:10

Ulises