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)



SQL Server Exception Message

 

I have seen many developers facing the above error on forums, hence I decided to explain the causes of the same.

Below is a Sample connection string

<connectionStrings>

    <add name="conString"

       connectionString="Data Source=MCN-1\SQLExpress;

                         database=Northwind;

                         Integrated Security=true" />

</connectionStrings>

 

One of the causes of this error comes in the incorrect connection string.

Mainly one should check.

1. Data Source or Server Name or IP address is Correct. For example here MCN-1. You can also put a .(dot) instead of machine name. For
    example .\SQLExpress

2. If any instance used check whether the instance name is correct. For example here SQLExpress

3. If any remote connections check if there is some firewall installed if yes disable it.

4. Also check if remote connections are configured in SQL Server. To configure SQL Server to allow remote connections read here. This article
    also explains how to create an exception in firewall in order to allow remote connections.

 

Finally also make sure that the SQL Server Service is running on the system. If it is stopped start it

The above helped me solve my issue. If someone else has anything other than above please feel free to share.