Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
2.15/5 (3 votes)
Hi,
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
Updated 13-Jun-17 9:37am
v2

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

The below url will give you more information about CommandTimeout.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtimeout.aspx[^]
 
Share this answer
 
Comments
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.

Thanks..
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.
Hi

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'.
 
Share this answer
 
Comments
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
{
get
{
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)
{
conn.Open();
}

return conn;
}
}
Can you give me the solution.
Thanks
 
Share this answer
 
Comments
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..

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