Click here to Skip to main content
16,021,041 members
Please Sign up or sign in to vote.
1.30/5 (3 votes)
See more:
I have one function in my project code like:

C#
public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
       {

           SqlCommand cmd = new SqlCommand();

           PrepareCommand(cmd, connection, null, cmdType, cmdText, commandParameters);
           object val = cmd.ExecuteScalar();
           cmd.Parameters.Clear();
           return val;
       }


I got an error saying Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.
on the line

C#
object val = cmd.ExecuteScalar(); 

This project has a large database and I know that I can't handle time period i not reach GUI .

one another function use for CommandTimeout property like

C#
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
       {

           if (conn.State != ConnectionState.Open)
               conn.Open();

           cmd.Connection = conn;
           cmd.CommandText = cmdText;
           cmd.CommandTimeout = conn.ConnectionTimeout;

           if (trans != null)
               cmd.Transaction = trans;

           cmd.CommandType = cmdType;

           if (cmdParms != null)
           {
               foreach (SqlParameter parm in cmdParms)
               {
                   if(parm.SqlValue == null) {parm.SqlValue = DBNull.Value; }
                   cmd.Parameters.Add(parm);
               }
           }
       }


Can u tell how to solve this problem or how I could increase the commandtimeout property?
Posted
Updated 13-May-11 1:10am
v2
Comments
Manfred Rudolf Bihy 13-May-11 7:32am    
Why the repost? I already gave you the solution.
Manfred Rudolf Bihy 13-May-11 7:39am    
Please explain what you mean by saying this: "This project has a large database and I know that I can't handle time period i not reach GUI"

Try below
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)

ref SqlC command cmd is the key. Original SqlCommand cmd will do nothing for input cmd since that is a local copy only.
 
Share this answer
 
v4
Comments
Richard Deeming 2-Nov-15 11:28am    
This adds nothing to the other answers posted OVER FOUR YEARS AGO!
Corrected your code:

C#
private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms, int commandTimeout)
       {

           if (conn.State != ConnectionState.Open)
               conn.Open();

           cmd.Connection = conn;
           cmd.CommandText = cmdText;
           // Set the timeout for your SqlCommand which was given as a parameter to the procedure
           cmd.CommandTimeout = commandTimeout; // a value of 120 seconds should be good for starters

           if (trans != null)
               cmd.Transaction = trans;

           cmd.CommandType = cmdType;

           if (cmdParms != null)
           {
               foreach (SqlParameter parm in cmdParms)
               {
                   if(parm.SqlValue == null) {parm.SqlValue = DBNull.Value; }
                   cmd.Parameters.Add(parm);
               }
           }
       }


You had set the same value as the connection timeout which is usually 15 seconds and may be enough to make a connection to the database but not for a longer SQL statement call.

Hope this clears your issue. I would make the value for the SqlCommand.CommandTimeout configurable via applications settings.

Best Regards,

-MRB
 
Share this answer
 
v2
Unless I'm missing something, just set the SqlCommand.CommandTimeout[^] property to the value you want it to wait for before it will report Timeout

// Wait for 1 minute for the query to execute before timing out
cmd.CommandTimeout = 60;

// Wait for 2 minutes for the query to execute before timing out
cmd.CommandTimeout = 120;

// Wait indefinitely for the query to execute
cmd.CommandTimeout = 0;


You shouldn't be doing this...

cmd.CommandTimeout = conn.ConnectionTimeout;


CommandTimeout is different to ConnectionTimeout. ConnectionTimeout is the amount of time to allow when establishing a connection to your datasource before aborting, while CommandTimeout refers to the execution of a particular command against the datasouce

http://devpinoy.org/blogs/joeycalisay/archive/2006/08/15/CommandTimeout-vs-ConnectionTimeout.aspx[^]
 
Share this answer
 
v2
Comments
Manfred Rudolf Bihy 13-May-11 7:42am    
Correct! 5+
I had already given OP the solution in his previous post. Obviously still not enough information for OP not to blunder!
rajjosh 13-May-11 8:23am    
hi, i am set the cmd.commandTimeout=120; but it not work, & also set in next attempt cmd.commandTimeout=0; but it also not work what can i do

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900