Understanding and Solving Named Pipes Provider Error: 40

Visual Studio emits a generic, verbose error message if it can’t connect to the SQL Server databse:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 – Could not open a connection to SQL Server)

The second sentence summarizes the issue: Visual Studio can’t connect to the SQL Server database. But why? Is it because the database is down? Is it a configuration error? What could it be?

Usually, this is caused by an incorrect or non-existent connection string. The first thing to do is check that you have a connection string defined. In Web.Config, you should see:



...




...

If you don’t have a connection string, or if it’s commented out, then add one. Above is a sample that tries to connect to the local database.

But wait! Data Source=(local) is not a best practice. Actually, you should use (as our error message hinted) Named Pipes to connect. The equivalent connection string, with named pipes, would look like:

In this case, we’ve replaced (local) with our SQL server instance name. We then need to double check that named pipes are enabled (they’re turned off by default in SQL Server).

To do that, load up the Sql Server Configuration Manager application. Under Sql Native Client 10.0 Configuration (32 bit), browse to Client Protocols, and set the Named Pipes attribute to true.

That’s all it takes! Restart SQL server, double-check your connection string, and you should be ready to go!

And if things still don’t work? SQL Server makes it quite difficult to connect to the database and verify that things are working (at least, with the pre-packaged tools). I recommend you download Toad for SQL Server. It will detect and connect to the local server, as well as give you the fully-qualified connecting string (machine name and instance name).

If you can view the database, tables, and run queries, then at least you know that SQL Server is running. Otherwise, you’re walking in the dark.

To summarize: this error can be caused by any number of reasons. The core message is that Visual Studio can’t connect to SQL Server. To remedy it, check your connection string, and use Toad for SQL Server to hit the database and make sure that it’s up and running (and to generate the connection string with the server and database instance names).

About Ashiq Alibhai, PMP

Ashiq has been coding C# since 2005. A desktop, web, and RIA application developer, he's touched ASP.NET MVC, ActiveRecord, Silverlight, NUnit, and all kinds of exciting .NET technologies. He started C# City in order to accelerate his .NET learning.
This entry was posted in Core .NET, Web and tagged , , . Bookmark the permalink.

5 Responses to Understanding and Solving Named Pipes Provider Error: 40

  1. Greg says:

    Nice explanation of a problem that I continue to deal with … thanks for the help.

  2. Pingback: Understanding and Solving Named Pipes Provider Error: 40 : C# City | NW Web Designs

  3. Pingback: Understanding and Solving Named Pipes Provider Error: 40 : C# City | NW Web Design

  4. Supriya Padale says:

    thanx!!!!!!!!!!!
    it worked.

  5. Mijo says:

    Thx, it helped me!

Leave a Reply

Your email address will not be published. Required fields are marked *