Reputation: 29
I have a question: how to deal with operation time out in asp.net MVC page. While loading the page which is quering the database SQL Server, it load the products. And sometime it says wait operation time out. How to deal with it.
Upvotes: 2
Views: 13386
Reputation: 839
In order to avoid wait operation timed out exception when running very long queries, change the SQL command properties and allow the command to wait longer before calling the timeout.
using System;
using System.Data.SqlClient;
///
public class A {
///
public static void Main() {
string connectionString = "";
// Wait for 5 second delay in the command
string queryString = "waitfor delay '00:00:05'";
using (SqlConnection connection = new SqlConnection(connectionString)) {
connection.Open();
SqlCommand command = new SqlCommand(queryString, connection);
// Setting command timeout to 1 second
command.CommandTimeout = 1;
try {
command.ExecuteNonQuery();
}
catch (SqlException e) {
Console.WriteLine("Got expected SqlException due to command timeout ");
Console.WriteLine(e);
}
}
}
}
Execution of Linq to sql query may take a longer time & exceed the default value of the DataContext class's CommandTimeout property. Default value is 30 sec. We can set the value of the CommandTimeout before each time a LINQ to SQL DataContext object is created and query invoked.
#region Constructors
/// <summary>
/// Initializes a new FrameworkEntities object using the connection string found in the 'FrameworkEntities' section of the application configuration file.
/// </summary>
public FrameworkEntities() : base("name=FrameworkEntities", "FrameworkEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
this.CommandTimeout = 300;
OnContextCreated();
}
/// <summary>
/// Initialize a new FrameworkEntities object.
/// </summary>
public FrameworkEntities(string connectionString) : base(connectionString, "FrameworkEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
this.CommandTimeout = 300;
OnContextCreated();
}
/// <summary>
/// Initialize a new FrameworkEntities object.
/// </summary>
public FrameworkEntities(EntityConnection connection) : base(connection, "FrameworkEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
this.CommandTimeout = 300;
OnContextCreated();
}
#endregion
Upvotes: 4