Tuesday, September 15, 2009

Code: Handling SQL Command Timeout Errors

Unfortunately there is not an exception object in .Net that corresponds directly to a command timeout.  However you can specifically handle one by checking the Number property on the exception.  The number for an SQL Exception happens to be –2 (Using reflection you can see the TdsEnum in System.Data.SqlClient).

catch (SqlException ex)
{
//Sql time out exception number is -2
if (ex.Number == -2)
{
//timeout exception, tell user to try changing params
lblTimeOut.Visible = true;
}
else
{
throw (ex);
}
}