Selected code is stored from code, runs fine with SSMS

I have a stored procedure that takes about 10 seconds to run when called from SSMS and succeeds. The procedure takes a parameter int

as a parameter.

When calling the same stored procedure from code:

using (var connection = new SqlConnection(ConnectionStringName))
{
    using (var cmd = new SqlCommand("ProcedureName", connection))
    {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new SqlParameter("@itemId", itemId));
        cmd.CommandTimeout = 150;

        connection.Open();
        cmd.ExecuteNonQuery(); 
    }
} 

      

The error I am getting is the following:

System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  
The timeout period elapsed prior to completion of the operation or the server is not responding. ---> 
System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

      

The passed parameter is valid and executes correctly when the stored procedure is called from SSMS with the same parameter value.

+3


source to share


2 answers


To avoid this error, just use:

cmd.CommandTimeout = 0;

      



Note:
The query is executed during the infinitive.

+2


source


You may have forgotten to specify the direction of the parameter, as you can specify input and output parameters. Try if it works:



SqlParameter param = new SqlParameter("@itemId", itemId);
param.Direction = ParameterDirection.Input;
cmd.Parameters.Add(param);

      

+2


source







All Articles