Saturday, March 30, 2013

A connection was successfully established with the server, but then an error occurred during the pre-login handshake

One of my .NET application threw SqlConnection exception with the following error.

"A connection was successfully established with the server, but then an error
occurred during the pre-login handshake."

This application used to work fine but all of sudden it stopped working. The code is pretty typical ADO.NET connection routine. It failed at conn.Open(). Interestingly, it only occurred when connecting to remote server. Connecting to local SQL server worked fine.

<pre>
  string connectionString = "Data Source=abc.com;Initial Catalog=testdb;User ID=user1;Password=1234;";
  using (SqlConnection conn = new SqlConnection(connectionString))
  {
      conn.Open();
      //.....
  }
</pre>

One explanation can be found in http://support.microsoft.com/kb/2568167.
I ran the following command to reset and then everything worked fine.

     C> netsh winsock reset

Of course, there will be so many different reasons/solutions for this same error. I just wanted to jot down one case of those.