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

.NetSql ServerSql Server-2008ado.netDatabase Connection

.Net Problem Overview


I am getting following error when i am trying to connect Production DB from Local Environment.

I was able to connect Production DB before, but suddenly i am getting following error, any idea?

>A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: TCP Provider, error: 0 - The handle is invalid.)

I was trying to run asp.net website in local PC, which has connection string of Production DB, following is stack trace for error I am getting in local environment.

>    at MyWebsiteDAL.clsForumQuestion.SelectAll(Int32 CurrentPageIndex, Int32 PageSize) in D:\EDrive\My WebSites\MyWebsite\MyWebsite\MyWebsiteDAL\clsForumQuestion.cs:line 821
       at CodeConnect.Default.Page_Load(Object sender, EventArgs e) in D:\EDrive\My WebSites\MyWebsite\MyWebsite\MyWebsite\Default.aspx.cs:line 100
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

Any idea what might have gone wrong here?

.Net Solutions


Solution 1 - .Net

Solution

  1. Clean your VS.Net Solution

  2. Rebuild Project.

  3. Reset IIS

  4. Run the project again.

Basically that solved my problem, but in my case i was not getting this error and suddenly my local environment starts giving me above error, so may be that trick work for me.

Solution 2 - .Net

In my case this error occurred with dot net core and Microsoft.Data.SqlClient. The solution was to add ;TrustServerCertificate=true to the end of the connection string.

Solution 3 - .Net

  • Save your work,
  • Close Visual Studio, then
  • Re-open your project

This worked for me.

Solution 4 - .Net

Running the following command worked for me:

netsh Winsock reset

Seen at https://serverfault.com/a/487139/250527

Solution 5 - .Net

I experienced this error when running some very memory-expensive processes. When the system started to run short of memory, I begun to notice this kind of error. I had to change the algorithm in order to make a better use of RAM.

To be noted that while some threads threw this exception, some other threw:

> System.Data.SqlClient.SqlException (0x80131904): Connection Timeout Expired. The timeout period elapsed while attempting to consume the pre-login handshake acknowledgement. This could be because the pre-login handshake failed or the server was unable to respond back in time. The duration spent while attempting to connect to this server was - [Pre-Login] initialization=43606; handshake=560; ---> System.ComponentModel.Win32Exception (0x80004005): The wait operation timed out

Both problems disappeared after the system was changed so that it could run using less RAM.

Solution 6 - .Net

I had the same problem, I was storing session data in the database, the connection string had Encrypt=True in it, which I assume told the sql client to connect to the server in secure (SSL) mode, removing this helped!

Solution 7 - .Net

You might want to check a few things:

  1. You production server allows remote connections. (possible that someone turned this off, especially if you have a DBA)

  2. Check your connection string. Sometimes if you are using an ip address or server name this will cause this error. Try both.

Solution 8 - .Net

In my case it was:

Persist Security Info=True;

in my connection string that needed to be removed. Once I did that I no longer had issues.

Solution 9 - .Net

As described in the answer of Ricardo ,

netsh Winsock reset

has worked for me ,

P.S. if you have Internet download manager or such programs which changes you IP Setting is installed then after running this command when you reboot your computer IDM will ask to change setting , Set NO in this case and then run your application it will work correctly.

Hope it

Solution 10 - .Net

I was getting the exact same problem with no changes to the code base or servers. It turned out to be that the DB server was running at 100% CPU and SQL Server was being starved of any CPU time, which caused the timeout.

Solution 11 - .Net

I had a similar issue where I couldn't connect to a database and tried the recommendations here.

At the end of the day this is what worked for me:

Used the SQL Server Configuration Manager tool to enable the TCP/IP and/or the Named Pipes protocols on the SQL Server client computer.

  1. Click Start, point to All Programs, and click SQL Server Configuration Manager.
  2. Click to expand SQL Server Network Configuration and then click Client Protocols.
  3. Right-click the TCP/IP protocol and then click Enable.
  4. Right-click the Named Pipes protocol and then click Enable.
  5. Restart the SQL server service if prompted to do so.

I am still not sure why or when this was disabled.

Solution 12 - .Net

Tried most of the above and nothing worked. Then turned off the VPN software (NordVPN) and everything was fine.

Solution 13 - .Net

I restarted SQL Server (Sharepoint) service and it solved the issue.

Solution 14 - .Net

For me the solution is to kill zombie IIS express worker processes.

e.g. locate in Task Manager and end task.

enter image description here

Solution 15 - .Net

I tried many solutions in the past month, none has worked. The problem is the Production DB is within a VPN and somehow the ISP provider thinks the HTTP connection is not secure. However, I was able to connect to the same Production DB from SSMS (uses TCP).

My solution: use the mobile phone as a hotspot and use mobile data instead of my office/home Wi-Fi.

Solution 16 - .Net

I had this same problem and was having no luck with the suggested fixes. I then came across this article and saw the comment from Mirrh regarding a program called Sendori blocking the LSP. No idea how it got on my computer but there it was and removing it fixed the issue.

If the article doesn't work just check your Programs and uninstall Sendori if you see it.

Solution 17 - .Net

Same problem here and no answers listed here worked, nor any solutions I could find online. The issue started shortly after Windows 10 anniversary update got applied on my dev PC and only affected my old SQL Server 2005 instance. I was not able to connect to the instance via my Web Applications or even using Sql Management Studio.

For what it's worth, this is what resolved it for me:

  1. Open SQL Server Configuration Manager (depending on what version of SQL Server you're running):
  • C:\Windows\SysWOW64\SQLServerManager.msc
  • C:\Windows\SysWOW64\SQLServerManager10.msc OR
  • C:\Windows\SysWOW64\SQLServerManager12.msc OR
  • etc
  1. Select SQL Server Services

  2. Locate the troubled service and view Properties

  • eg SQL Server (SQL2005) in my case
  1. In the Log On tab, change the "Built-in account" to "Network Service"

Which is almost what this random solution said: http://www.kutayzorlu.com/operating-systems/linux-unix-redhat-debian-ubuntu-opensuse-centos/general-server-administrating/error-fixed-a-connection-was-successfully-established-with-the-server-but-then-an-error-occurred-during-the-pre-login-handshake-12405.html

I chose Network Service for no reason at all. Mine was already configured to use Local System. This security doesn't matter to me as it was only problematic on my Local development machine, only accessed locally. I can't advise why this works, but it did.

Solution 18 - .Net

In my case, i was getting the error when i wanted to access a remote database. However, i solved it by starting SQL Server Browser service.

enter image description here

Solution 19 - .Net

Had the same problem from many days. I had to explicitly add TLS1.2 support in my core project to address this error and it worked fine. ( ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;)

See below link for more details (thanks to author Usman Khurshid) https://www.itechtics.com/connection-successfully-established-error-occured-pre-login-handshake/

Solution 20 - .Net

If you are connecting to an old SQL Server:

Switch from System.Data.SqlClient.SqlConnection to System.Data.OleDb.OleDbConnection

Use an OleDb connection string:

<connectionStrings>
<add name="Northwind" connectionString="Provider=SQLOLEDB.1; Data Source=MyServer; Initial Catalog=Northwind;  Persist Security Info=True; User ID=abc; Password=xyz;" providerName="System.Data.OleDb" />
</connectionStrings>

Solution 21 - .Net

In my case, the connection from my API to the DB worked fine when running on my local machine, this error only occurred when running the .NET Core 5 API application in a docker container utilizing base image: mcr.microsoft.com/dotnet/aspnet:5.0. The problem was a TLS version inconsistency between SQL Server and one of our Dependencies. The fix was to add the following line to the API Dockerfile, downgrading security level of TLS:

RUN sed -i 's/DEFAULT@SECLEVEL=2/DEFAULT@SECLEVEL=1/g' /etc/ssl/openssl.cnf

This was taken from this article wherein the aforementioned RUN command was suggested, which fixed my problem. Direct link to the suggested fix is here.

Solution 22 - .Net

Had the same issue, the reason for it was BCrypt.Net library, compiled using .NET 2.0 framework, while the whole project, which used it, was compiling with .NET 4.0. If symptoms are the same, try download BCrypt source code and rebuild it in release configuration within .NET 4.0. After I'd done it "pre-login handshake" worked fine. Hope it helps anyone.

Solution 23 - .Net

I experienced this error and did all the suggestions from you guys here but none had any effect for my error.

I caught the culprit: if you are using *.ini file for your system, you might want to check what server name was input there and make sure it is the same as the one in your web.config connection string.

Solution 24 - .Net

In my case, Re-enabling TLS 1.0 in the DB server resolved this issue.

Solution 25 - .Net

In my case, it was a TLS protocol mismatch between a Raspberry Pi hosting a .Net Core 3.1 app requesting to a Windows Server 2012 with MSSQL. I tried many changes of TLS protocol version on the Windows Server side without any effect. I finally found this post https://github.com/dotnet/SqlClient/issues/126, indicating modify the client side, ie to update the /etc/ssl/openssl.cnf on the RPi side, and that worked great for me.

Solution 26 - .Net

For me I had this happen on a HyperV virtual machine that was accessing my database on my local (host) machine. Basically it all works normally, but after a reboot there is some weird condition that this error starts coming up from the virtual machine applications. There is something in the network layer that is getting horribly confused. However, I have found if I ping the virtual server from the host server, it seems to fix the problem (not sure why exactly). But this might help someone.

Solution 27 - .Net

Another thing to check is if you have your SQL Server Network Configuration set to 'Force Encryption'. If you do, you'll get this error unless you do the requisite client incantations for an encrypted connection.

Solution 28 - .Net

#macusers

I have found new solution without closing the Visual Studio.

  1. Go to the terminal.

  2. lsof -i:<PORT_Number> i.e. 1433 in my case.

  3. Outout will be process using that port number

  4. Kill those processes using kill -9 <PID> i.e. process Id

Please refer Screenshot attached.

commands and respective outputs for killing process

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJordon WillisView Question on Stackoverflow
Solution 1 - .NetJordon WillisView Answer on Stackoverflow
Solution 2 - .NetgratiniererView Answer on Stackoverflow
Solution 3 - .NetDumisaniView Answer on Stackoverflow
Solution 4 - .NetRicardo StuvenView Answer on Stackoverflow
Solution 5 - .NetStarnuto di topoView Answer on Stackoverflow
Solution 6 - .NetTerry KernanView Answer on Stackoverflow
Solution 7 - .NetRichard FantozziView Answer on Stackoverflow
Solution 8 - .NetdelliottgView Answer on Stackoverflow
Solution 9 - .NetVishal SharmaView Answer on Stackoverflow
Solution 10 - .NetTheLukeMcCarthyView Answer on Stackoverflow
Solution 11 - .NetdaffyjejeView Answer on Stackoverflow
Solution 12 - .NetPaul EvansView Answer on Stackoverflow
Solution 13 - .Netuser2087459View Answer on Stackoverflow
Solution 14 - .NetDaniel de ZwaanView Answer on Stackoverflow
Solution 15 - .Netmihai_omegaView Answer on Stackoverflow
Solution 16 - .NetrickeroyeView Answer on Stackoverflow
Solution 17 - .NetArkiliknamView Answer on Stackoverflow
Solution 18 - .NetIfesinachi BryanView Answer on Stackoverflow
Solution 19 - .NetPhantomView Answer on Stackoverflow
Solution 20 - .NetDominic IsaiaView Answer on Stackoverflow
Solution 21 - .NetThomasView Answer on Stackoverflow
Solution 22 - .NetEadelView Answer on Stackoverflow
Solution 23 - .Netuser3619399View Answer on Stackoverflow
Solution 24 - .NetChinthaka FernandoView Answer on Stackoverflow
Solution 25 - .NetP.LafView Answer on Stackoverflow
Solution 26 - .NetKeith NicholasView Answer on Stackoverflow
Solution 27 - .NetAlan BView Answer on Stackoverflow
Solution 28 - .NetTejashreeView Answer on Stackoverflow