2 Replies - 3580 Views - Last Post: 10 March 2011 - 06:19 AM

#1 krista80  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 22-February 11

Cannot connect to SQL Server Error: 40

Posted 09 March 2011 - 06:45 AM

Hi. I am currently getting this error every time I try and connect to the server:

A network-related or instance-specific error occured 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 line that the error occurs on is this:
sqlConn.Open();


This is how it looks in my code:
using (SqlConnection sqlConn = new SqlConnection(connectionString))
{
    sqlConn.Open();
    //more code after this but not necessary for this question
}


My connection string stuff looks like this:

From the C# code file behind the ASP.NET page:
string connectionString = WebConfigurationManager.ConnectionStrings["connectionstringname"].ConnectionString;

From the Web.config file:
  <connectionStrings>
    <add name="connectionstringname" connectionString="Data Source=localhost;Initial Catalog=databasename;Integrated Security=SSPI" />
    <add name="connectionstringnameConnectionString" connectionString="Data Source=servername;Initial Catalog=databasename;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>


I have checked the server, and it DOES allow remote access. I am using SQL Server 2005, rather than SQL Server 2008, what I was using on my laptop, where the program worked. Can anyone help? Also I am a bit confused over what an instance is in reference to SQL Server. Is it the name of the server? Or the name of the database? Or the name of the computer the server is on?

Any help is greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Cannot connect to SQL Server Error: 40

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: Cannot connect to SQL Server Error: 40

Posted 09 March 2011 - 04:05 PM

You can think of instance as a copy of the running program. You can start up and have multiple instances running. That is, have copies of sql server open and running their own databases independent of one another. Usually you pick an instance name when you install a copy of sql server and it is unique to that particular installation copy of the service. When you connect to one of these running services, you are said to be connecting to a specific instance of sql server.

Now as for your error... have you made sure that your sql server is actually running in the background of the target machine? If it is not started and running (look in your list of services) it will have trouble connecting to it. Secondly, have you made sure that you are connecting to the right port and that your firewalls are not at all blocking this port? You should be looking at port 1433 for the default setting.

If you went from a local copy on your machine to a copy on a remote server, you will also not want it to be localhost. You will want to specify the server on the network or you could even try the IP address. localhost would be only useful if the server can be seen running local to the code running... that is, the server is running on the same machine you are serving files from.

Hope this helps! :)
Was This Post Helpful? 1
  • +
  • -

#3 krista80  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 25
  • Joined: 22-February 11

Re: Cannot connect to SQL Server Error: 40

Posted 10 March 2011 - 06:19 AM

Thank you so much, Martyr2! It works now. All I had to do was do as you suggested and change the local host data source to the server name! Thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1