0

I have a C# ASP.NET MVC program which runs a stored procedure while loading a page. If I run the stored procedure from Microsoft SQL Server Management Studio, it takes 1 minute. However, if I try to run the same stored procedure from code, it times out. I have added Connection Timeout=0 in web.config, but sometimes it works, sometimes not.

2
  • 1
    Try to set the Connection Timeout=0; in connectionString of your web.config file Commented Dec 31, 2018 at 12:00
  • I have added Connection Timeout=0 in web.config, but sometimes it works, sometimes not. Commented Jan 1, 2019 at 11:48

1 Answer 1

0

You can set the Timeout Command to 0 when you are calling stored procedure.

using (SqlConnection connection = new SqlConnection(connectionString)) 
{
   connection.Open();  
   SqlCommand cmd= new SqlCommand(queryString, connection);  
   // Setting command timeout to 0 second  
   cmd.CommandTimeout = 0;  
   try 
   {  
     cmd.ExecuteNonQuery(); 
   }  
   catch(Exception ex)
   {
      // log ex here
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.