Entity Exception : the underlying provider failed to open with Timeout expired

Go To StackoverFlow.com

0

OK, I'm stumped. I am trying to create a .NET Winforms app to talk to a database via Entity Framework. I originally created a ASP.NET MVC3 app (with a seperate DAL layer) that can talk to the database after initially failing. SO the first time that it tries to open the connection, it times out. If I then hit F5 in the browser, it connects fine! I referenced this DAL in my Winforms app.

  • I can connect to the database via SQL Auth in SQL Management Studio fine.
  • I can create the ADO.NET Data classes in VS2010 (it connects, gets the tables and stores the connection string in the app.config).
  • However, once I run the Winforms app, I am unable to connect. I get the mentioned exception with an InnerException of "Timeout expired."

I copied the connection string from the web.config to the app.config.

The SQL database is hosted on my Windows Home Server called SERVER. I am in a Workgroup (no Domain). I uses SERVER\SQLEXPRESS in Management Studio and VS to successfully connect to it.

Things I've tried:

  • Disable the firewall on the client machine.
  • Enabled DTC on the server and added msdtc.exe to the exclusion list on the server.
  • Made sure Named Pipes in enabled on the SQL server instance.
  • Tried different SQL users.
  • Tried different ways of constructing the connection in C#.
  • Generated a new ADO.NET Data class local to my WinForms project (not referencing the DAL). VS connects fine, reads the tables, generates the code. But the running app causes a "Timeout Expired" SqlException.

So running the app is unsuccessful in connecting. When I watch the app in ProcessExplorer I can see an entry in the TCP window when it tries to Connect on remote port 4845 with a SYN_SENT message. Not sure if that's right.

I've google'd and bing'd this for the last few days, I've read all the articles and replies here on Stackoverflow that mention this exception, but I have hit a wall.

This is the ConnectionString that VS generated after reading the DB:

<add name="LLDBEntities" connectionString="metadata=res://*/LLDB.csdl|res://*/LLDB.ssdl|res://*/LLDB.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=SERVER\SQLEXPRESS;initial catalog=LLDB;persist security info=True;user id=sa;password=sapwd;multipleactiveresultsets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

Any help appreciated, even any hints on how to figure out what's happening.

2012-04-04 21:29
by Lutz Kretzschmar
You may want to change your sa password : - Shiraz Bhaiji 2012-04-04 21:45
That's just a placeholder... :-) It's not the actual one - Lutz Kretzschmar 2012-04-04 21:58


1

Ok, solved.

I noticed that it would actually connect on the second attempt with the same app and connection. At that point I found this link: http://kromey.us/2011/06/microsoft-sql-server-2008-times-out-on-first-connection-attempt-447.html

So I opened the firewall port listed under IPAll Dynamic Port in SQL Server Configuration Manager on the TCP/IP Properties for the instance and now it all works fine. I still don't know why it would get through on the second attempt....

2012-04-06 00:39
by Lutz Kretzschmar


1

Your connection string looks more like a connection string to an SQL Server, rather than SQL Express.

Have a look at this for a connection string to SQL Express: SQL Server Express connection string for Entity Framework Code First

2012-04-04 22:30
by Shiraz Bhaiji
Hmmm. That page says to have a "Database=" or "Initial Catalog=" in my ConnectionString, which I do. Note that the SQL Server is on another machine. If I add the "AttachDBFilename=" fields, the connection fails with "... provider failed to open on ConnectionString." - Lutz Kretzschmar 2012-04-04 22:42
Are you using windows authentication when you access the database via the SQL Management Studio - Shiraz Bhaiji 2012-04-05 09:04
No, SQL Auth, as mentioned - Lutz Kretzschmar 2012-04-05 17:17
Ads