SQL Connectivity error from windows server 2012 r2 RRS feed

  • Question

  • User-824961 posted
    Connection string :
    data source=xxxx.xx.xxxx.com,2048;Initial Catalog=testdatabase;User Id =ghdgfg;Password=yet.we-2
    Platform : windows server 2012 r2
    .NET Framework 4.0
    Applied many sql server patches suggested by microsoft still same issue.
    We have 2 servers with same configuration. In both the server TLS 1.2 & 1.0 is enabled. In one server application to sql server connectivity works from C# code. 
    In another server we are getting the below error. Need your help to provide the solution why in one server its throwing this error.
    "An existing connection was forcibly closed by the remote host "
     at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
       at System.Data.SqlClient.TdsParserStateObject.SNIWritePacket(SNIHandle handle, SNIPacket packet, UInt32& sniError, Boolean canAccumulate, Boolean callerHasConnectionLock)
       at System.Data.SqlClient.TdsParserStateObject.WriteSni(Boolean canAccumulate)
       at System.Data.SqlClient.TdsParserStateObject.WritePacket(Byte flushMode, Boolean canAccumulate)
       at System.Data.SqlClient.TdsParser.TdsLogin(SqlLogin rec, FeatureExtension requestedFeatures, SessionData recoverySessionData)
       at System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover)
       at System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)
       at System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance)
       at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData)
       at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
       at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
       at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
       at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
       at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
       at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
       at System.Data.SqlClient.SqlConnection.Open()
       at UserAccessManagement.Code.GeneralMethods.GetDataToLoadDll(String tableName, String colName1, String colName2)
    C# Code: This code is working in 1 server and failing in another server with above error.
     private static SqlConnection con;
     con = new SqlConnection(EncryptDecrypts.Decrypt(ConfigurationManager.AppSettingsconnectionstringtrue));
     cmd = new SqlCommand();
     cmd.Connection = con;
     SqlDataAdapter da = new SqlDataAdapter(cmd);
     DataTable dt = new DataTable();

    Thursday, November 14, 2019 8:54 AM

All replies

  • User753101303 posted


    Do they have the same firewall configuration and/or both servers are authorized on the db side firewall if any ?

    "An existing connection was forcibly closed by the remote host" means basically the network request is sent but later blocked explicitely while en route to the target server, most often because of a firewall config.

    Thursday, November 14, 2019 11:53 AM
  • User-719153870 posted

    Hi santosh-hegde,

    "An existing connection was forcibly closed by the remote host "

    As @PatriceSc mentioned, this issue can usually caused by the firewall configuration, please make sure the firewall configuration of your servers are the same.

    However, i think it is also possible that the error is caused by your project configuration since it is related to System.Data.SqlClient.

    First, i recommend you could debug your program and locate which line caused the error.

    Second, you can check your c# code since we can't see you close the connection by con.Close() after you did con.Open() or maybe you just didn't provide the complete code. Use a using statement will be even better.

    Also, i found a similar case at What is the cause of this error? you met really similar situation that maybe you can check it for answer since there's a MVP telling how to figure out the question where you can follow. At the end of that thread, op solved his problem by adding MultipleActiveResultSets=true to his webconfig's connectionstring.

    Last but not least, you really should modify your post since it's really hard to read which makes the community harder to help.

    Best Regard,

    Yang Shen

    Friday, November 15, 2019 2:35 AM