SqlCommand CommandTimeout property

Go To StackoverFlow.com


i have developed a web application asp.net C#.

on a button click i am connecting to the database which takes long time to execute. initially it use to timeout. now i increased the commondtimeout property ,

cmd.CommandTimeout = 500;

basically it processing of paygroups. time required depends on num of employess in that paygroup.

so my question is if 1 of paygroup requires less time to execute say 2 or 3 mins, will the connection sill be open for specified time in commandtimeout property?

if yes . can we estimate the processing time and set the time accordingly .

2012-04-04 05:57
by kumar chaudhari
but why do you want to set lesser time? set the upper bound onl - Shoaib Shaikh 2012-04-04 05:59
FYI, the language is named "C#", not "csharp" - John Saunders 2012-04-04 06:03
CommandTimeout is an upper threshold - if the command completes in less time, any synchronous code will continue as soon as the command completes - StuartLC 2012-04-04 06:10
Please check new answer - Pankaj 2012-04-11 11:10


why dont you try out something like this

using (SqlConnection cn = new SqlConnection(connectionString)) 
  //your codee to perform database operation

this will close your connection once your work get completed.

2012-04-04 06:01
by Pranay Rana
does this requires to commandtimeout property - kumar chaudhari 2012-04-04 06:08
@kumarchaudhari - you can set the value there no issue in it but it remain for this scop only. - Pranay Rana 2012-04-04 06:33


  1. In Order to let the Query execute completely, Set the CommandTimeOut = 0.
  2. Why It is taking so much time? Your transaction should be quick enough to let other query execute in time.
  3. SQL Profiler can help you optimize your database queries. I assume you are using SQL Server
2012-04-04 07:31
by Pankaj