Click here to Skip to main content
13,142,182 members (54,761 online)
Rate this:
Please Sign up or sign in to vote.
We are using windows application in c#.NET 4.0 with SQL server 2005. We got the Timeout expired exception while establishing the connection to the SQL server.Can anyone know the reason why we are getting this type of exception.

Stack Trace is as follows:
System.Data.SqlClient.SqlException (0x80131904): Timeout expired.  The timeout period elapsed prior to completion of the operation or the server is not responding.
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParserStateObject.ReadSniError(TdsParserStateObject stateObj, UInt32 error)
   at System.Data.SqlClient.TdsParserStateObject.ReadSni(DbAsyncResult asyncResult, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParserStateObject.ReadNetworkPacket()
   at System.Data.SqlClient.TdsParser.ConsumePreLoginHandshake(Boolean encrypt, Boolean trustServerCert, Boolean& marsCapable)
   at System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity)
   at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, SqlConnection owningObject)
   at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, Boolean redirectedUserInstance, SqlConnection owningObject, SqlConnectionString connectionOptions, TimeoutTimer timeout)
   at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(SqlConnection owningObject, TimeoutTimer timeout, SqlConnectionString connectionOptions, String newPassword, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, String newPassword, SqlConnection owningObject, Boolean redirectedUserInstance)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection owningConnection, DbConnectionPool pool, DbConnectionOptions options)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)
   at System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection)
   at System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory)
   at System.Data.SqlClient.SqlConnection.Open()

Thanks in Advance.
Posted 23-Nov-12 0:55am
Updated 13-Jun-17 9:37am
Rate this: bad
Please Sign up or sign in to vote.

Solution 1


Did you set the CommandTimeout ? . If not can you set the CommmandTimeout and check ?

The below url will give you more information about CommandTimeout.[^]
sravanthi25 23-Nov-12 7:36am
Thank you for your response. We are tried with the CommandTimeout property. We set to zero.
This fix is for long term or temporary usage.
Because we are not getting this exception very frequently. Can you suggest me that any other solution or this solution is enough.

Dominic Abraham 23-Nov-12 11:36am
If the solution is helpful don't forget to mark it as answer. So that it may help others.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

Dominic Abraham 26-Nov-12 1:14am
Your issue solved?
sravanthi25 26-Nov-12 2:29am
Sorry for late replay.
Thank you so much...
My problem is fixed..
Rate this: bad
Please Sign up or sign in to vote.

Solution 2


It is not advisable to set the value to zero. You set some value after checking the execution time of your biggest query. If your biggest query takes 10 minutes to execute , set the value to '600'. If you have 1000 queries, and out of that one query takes 20 mints and all others takes 1 mint, what i do is set the value to '60' and identify the particular query, if that particular query is going to execute, i will set the value to '20 * 60'.
sravanthi25 25-Nov-12 23:36pm
Thank you for your response. Unfortunatly the above exception is raised while establishing the connection to server [i.e., conn.Open() ] but not from command object.
My code is look like below:

SqlCommand cmdSqlCommand = new SqlCommand();

cmdSqlCommand.Connection = Connection; // exception is raised form here. Here
Connection is the Public property.

cmdSqlCommand.CommandType = CommandType.StoredProcedure;

cmdSqlCommand.CommandText = StoredProcedureName;

int result = cmdSqlCommand.ExecuteNonQuery();// exception is not raised from here

//"Connection" Property

public SqlConnection Connection
if (conn == null)
string connstr = "server=localhost\SQLEXPRESS;Initial Catalog=MYDB;Integrated Security=True";
conn = new SqlConnection(connstr);

if (conn.State == System.Data.ConnectionState.Closed)

return conn;
Can you give me the solution.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.170915.1 | Last Updated 13 Jun 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100