title: provider: SSL Provider, error: 0 - An existing connection was forcibly closed by the remote host
body: <p>In short, I wrote a console app that just calls a stored procedure in a SQL Server database. Then setup a Windows Task Scheduler event to call the console app's .exe file every 5 minutes. Most of the time it works, however about 10 times a day or so we get the following sqlException as soon as the connection is opened to call the stored procedure:</p>
  
  <pre><code>System.Data.SqlClient.SqlException (0x80131904): A connection was successfully established with the server, 
  but then an error occurred during the login process. 
  (provider: SSL Provider, error: 0 - An existing connection was forcibly closed by the remote host.) 
  </code></pre>
  
  <p>I've temporarily built in a try block with a simple 5 retries and a short pause between and it's helped some but there's still several times a day that fail out all 5 times. I'm thinking it's got to be some sort of communication issue but I'm not even sure where to look.</p>
  
  <p>Most of the research I've done points towards TLS or firewall issues. I'm not an expert at either of those but I would think if either of those were the cause it would get the error every time consistently. Same thing with the reference to SSL. On the other hand for the same reason I don't see how it could be a coding issue with either the console app or the stored procedure so I'm out of ideas. </p>
  
  <p>The SQL connection code is something like the following:</p>
  
  <pre><code>
      Public Function GetDataTable(tsql As String, Optional ConnectionString As String = Nothing, Optional TryNum As Integer = 1) As DataTable
          Try
              Dim dt As DataTable
              Using conn As SqlConnection = New SqlConnection(If(Not ConnectionString Is Nothing, ConnectionString, SConn))
                  conn.Open()
                  Using dr As SqlDataReader = (New SqlCommand With {.CommandType = CommandType.Text, .CommandText = tsql, .Connection = conn, .CommandTimeout = SqlCommandTimeout}).ExecuteReader
                      dt = New DataTable()
                      dt.Load(dr)
                      dr.Close()
                  End Using
                  conn.Close()
              End Using
              Return dt
          Catch exSQL As System.Data.SqlClient.SqlException
              If exSQL.ToString.Contains("A connection was successfully established with the server, but then an error occurred during the login process.") AndAlso TryNum &lt; MaxTries Then
                  Threading.Thread.Sleep(RetryWaitMS)
                  Return GetDataTable(tsql, ConnectionString, TryNum + 1)
              Else
                  SendMsg(ERROR_EMAIL_GROUP, ERROR_EMAIL_GROUP, "CommonUtils - SQL Error", tsql &amp; vbCrLf &amp; exSQL.ToString)
                  Throw exSQL
              End If
          End Try
      End Function
  </code></pre>
  
  <p>Please don't start in about not using Parameters.AddWithValue etc and SQL Injection. </p>