13

When I am running a query in web application, I'm getting a null value. Same query directly in SQL Management Studio returns results.

I think that the problem is a timeout. How can I increase the time for execution of query in web application? In my web.config : connectionstring, there is no code for timeout. If I choose a timeout there, will that affect other parts of my system?

Abel
  • 56,041
  • 24
  • 146
  • 247
Jui Test
  • 2,399
  • 14
  • 49
  • 76
  • 1
    It cannot be controlled by configuration. Set CommandTimeout of SqlCommand. For learning more about page request timeout settings see the post http://stackoverflow.com/questions/7804622/how-to-upload-content-more-than-2-mbs-on-website-created-using-asp-net-4-0/7804670#7804670 – Prasanth Nov 02 '11 at 07:06
  • 1
    How do you know its because of timeout? Try using Sql Profiler and see what query is being formed. See other details like the duration, reads etc. Pull the query from profiler and then execute, that query in Sql Server. – Pawan Mishra Nov 02 '11 at 07:09
  • Iam using sql server 2005 management studio.Where is profiler.I don't know how to ust it? – Jui Test Nov 03 '11 at 07:33

5 Answers5

13

You can do one thing.

  1. In the AppSettings.config (create one if doesn't exist), create a key value pair.
  2. In the Code pull the value and convert it to Int32 and assign it to command.TimeOut.

like:- In appsettings.config ->

<appSettings>    
   <add key="SqlCommandTimeOut" value="240"/>
</appSettings>

In Code ->

command.CommandTimeout = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SqlCommandTimeOut"]);

That should do it.

Note:- I faced most of the timeout issues when I used SqlHelper class from microsoft application blocks. If you have it in your code and are facing timeout problems its better you use sqlcommand and set its timeout as described above. For all other scenarios sqlhelper should do fine. If your client is ok with waiting a little longer than what sqlhelper class offers you can go ahead and use the above technique.

example:- Use this -

 SqlCommand cmd = new SqlCommand(completequery);

 cmd.CommandTimeout =  Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["SqlCommandTimeOut"]);

 SqlConnection con = new SqlConnection(sqlConnectionString);
 SqlDataAdapter adapter = new SqlDataAdapter();
 con.Open();
 adapter.SelectCommand = new SqlCommand(completequery, con);
 adapter.Fill(ds);
 con.Close();

Instead of

DataSet ds = new DataSet();
ds = SqlHelper.ExecuteDataset(sqlConnectionString, CommandType.Text, completequery);

Update: Also refer to @Triynko answer below. It is important to check that too.

  • This will help you change the value whenever you want. But if you are planning for huge data, I suggest doing more analysis on the nature of business and how the data may accumulate, and then plan your Db design. As the timeout required may increase with increasing number of records and complexity involved. Please discard this comment if you already have a DB created or not allowed to architect. –  Apr 24 '15 at 06:44
3

I realise I'm a litle late to the game, but just spent over a day on trying to change the timeout of a webservice. It seemed to have a default timeout of 30 seconds. I after changing evry other timeout value I could find, including:

  • DB connection string Connect Timeout
  • httpRuntime executionTimeout
  • basicHttpBinding binding closeTimeout
  • basicHttpBinding binding sendTimeout
  • basicHttpBinding binding receiveTimeout
  • basicHttpBinding binding openTimeout

Finaley I found that it was the SqlCommand timeout that was defaulting to 30 seconds.

I decided to just duplicate the timeout of the connection string to the command. The connection string is configured in the web.config.

Some code:

namespace ROS.WebService.Common
{
  using System;
  using System.Configuration;
  using System.Data;
  using System.Data.SqlClient;

  public static class DataAccess
  {
    public static string ConnectionString { get; private set; }

    static DataAccess()
    {
      ConnectionString = ConfigurationManager.ConnectionStrings["ROSdb"].ConnectionString;
    }

    public static int ExecuteNonQuery(string cmdText, CommandType cmdType, params SqlParameter[] sqlParams)
    {
      using (SqlConnection conn = new SqlConnection(DataAccess.ConnectionString))
      {
        using (SqlCommand cmd = new SqlCommand(cmdText, conn) { CommandType = cmdType, CommandTimeout = conn.ConnectionTimeout })
        {
          foreach (var p in sqlParams) cmd.Parameters.Add(p);
          cmd.Connection.Open();
          return cmd.ExecuteNonQuery();
        }
      }
    }
  }
}

Change introduced to "duplicate" the timeout value from the connection string:CommandTimeout = conn.ConnectionTimeout

kesse
  • 160
  • 8
3

SQL Server has no setting to control query timeout in the connection string, and as far as I know this is the same for other major databases. But, this doesn't look like the problem you're seeing: I'd expect to see an exception raised

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

if there genuinely was a timeout executing the query.

If this does turn out to be a problem, you can change the default timeout for a SQL Server database as a property of the database itself; use SQL Server Manager for this.

Be sure that the query is exactly the same from your Web application as the one you're running directly. Use a profiler to verify this.

Jeremy McGee
  • 24,842
  • 10
  • 63
  • 95
  • are the settings in SQL Management Studio still for itself instead of for SQL Server? (i suspect so) – symbiont Feb 12 '19 at 10:57
2

You should add the httpRuntime block and deal with executionTimeout (in seconds).

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
...
 <system.web>
   <httpRuntime executionTimeout="90" maxRequestLength="4096"
    useFullyQualifiedRedirectUrl="false"
    minFreeThreads="8"
    minLocalRequestFreeThreads="4"
    appRequestQueueLimit="100" />
 </system.web>
... 
</configuration>

For more information, please, see msdn page.

maxk
  • 642
  • 1
  • 9
  • 21
  • 4
    This is the timeout for the execution of the ASP.NET page itself, not for the SQL query. – Abel Apr 04 '13 at 14:06
  • 1
    The httpRuntime executionTimeout should be probably be at least as long as the query execution timeout if you're running the query as part of a request, otherwise the request will timeout and be cancelled before the query has a chance to complete. – Triynko Sep 23 '15 at 18:27
1

I think you can't increase the time for query execution, but you need to increase the timeout for the request.

Execution Timeout Specifies the maximum number of seconds that a request is allowed to execute before being automatically shut down by ASP.NET. (Default time is 110 seconds.)

For Details, please have a look at https://msdn.microsoft.com/en-us/library/e1f13641%28v=vs.100%29.aspx

You can do in the web.config. e.g

<httpRuntime maxRequestLength="2097152" executionTimeout="600" />
Off The Gold
  • 1,228
  • 15
  • 28
Muhammad Akhtar
  • 51,913
  • 37
  • 138
  • 191