0

I am running a stored procedure but while running it shows error like

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

I have set my web config like

   <add name="SAMPLECONNECTION" connectionString="Data Source=ADMIN\SQLEXPRESS;Initial Catalog=PFGRT;Persist Security Info=True;MultipleActiveResultSets=true;User ID=sa;connect Timeout =1000000;Password=PASSWORD"
  providerName="System.Data.SqlClient" />

I tried like set the connect Timeout = 0; also, but I get the same error.

How can I solve it?

My Stored Procedure calling class

public DataSet adFeedBackReportFacultyWise(string SetID, string FacultyID, string SubjectID,string groupid)
{
    DbUtil du = new DbUtil();
    Dictionary<string, SqlParameter> cmdParameters = new Dictionary<string, SqlParameter>();
    cmdParameters["SetID"] = new SqlParameter("SetID", SetID);
    cmdParameters["FacultyID"] = new SqlParameter("FacultyID", FacultyID);
    cmdParameters["SubjectID"] = new SqlParameter("SubjectID", SubjectID);
    cmdParameters["groupid"] = new SqlParameter("groupid", groupid);

    return du.ExecuteQuery("adFeedBackReportFacultyWise", cmdParameters);
}

My DButil Class is

public class DbUtil
{
    string connection = ConfigurationManager.ConnectionStrings["gatikkcmsConnectionString"].ConnectionString;

    public SqlDataAdapter ad;
    public DataTable dt = new DataTable();
    SqlConnection conn;
    SqlCommand cmd = new SqlCommand();
    public DbUtil()
    {
        conn = new SqlConnection(ConfigurationManager.ConnectionStrings["gatikkcmsConnectionString"].ConnectionString);
        cmd = null;
    }

    public SqlConnection getConnection()
    {
        return new SqlConnection(connection);
    }
   public int ExecuteCommand(string storedProcName, Dictionary<string, SqlParameter> procParameter)
    {
        int rc;
        using (SqlConnection cn = getConnection())
        {
            cn.Open();
            // create a SQL command to execute the stored procedure
            using (SqlCommand cmd = cn.CreateCommand())
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = storedProcName;
                // assign parameters passed in to the command
                foreach (var procParameters in procParameter)
                {
                    cmd.Parameters.Add(procParameters.Value);
                }
                rc = cmd.ExecuteNonQuery();
            }
            cn.Close();
        }
        return rc;
    }
}
Semil Sebastian
  • 519
  • 2
  • 9
  • 24

2 Answers2

5

The issue is not the connection timeout (time taken to establish a connection to the database server), but rather the command timeout (time taken to execute the command).

In your DbUtil class, set the command timeout against the SqlCommand like so:

using (SqlCommand cmd = cn.CreateCommand())
{
    cmd.CommandTimeout = 120; //set timeout - use appropriate value
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = storedProcName;
    // assign parameters passed in to the command
    foreach (var procParameters in procParameter)
    {
        cmd.Parameters.Add(procParameters.Value);
    }
    rc = cmd.ExecuteNonQuery();
}

It would be worthwhile checking the performance of the stored procedure as well.

Brendan Green
  • 11,676
  • 5
  • 44
  • 76
0

As you are facing a timout on your command, therefore you need to increase the timout of your sql command. You can specify it in your command like this

// Setting command timeout to 2 minutes
cmd.CommandTimeout = 120;
Krsna Kishore
  • 8,233
  • 4
  • 32
  • 48