Click here to Skip to main content
15,905,776 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi, I am recieving the follwing error on a piece of code linking to a database:

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)

My code is:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;
using System.Data.SqlClient;

namespace WpfApplication1
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window

        public MainWindow()

        public void dbCC(){
        // 1. Instantiate the connection
        SqlConnection conn = new SqlConnection(
            "Data Source=(local);Initial Catalog=Northwind");

        SqlDataReader rdr = null;

            // 2. Open the connection

            // 3. Pass the connection to a command object
            SqlCommand cmd = new SqlCommand("select * from Customers", conn);

            // 4. Use the connection

            // get query results
            rdr = cmd.ExecuteReader();

            // print the CustomerID of each record
            while (rdr.Read())
            // close the reader
            if (rdr != null)

            // 5. Close the connection
            if (conn != null)

        private void button3_Click(object sender, RoutedEventArgs e)


This is my first time using a database in c#, this is obviously just a simple test to connect to a db and retrieve some data, but I seem to be stuck at the first hurdle and I haev no idea why!!

Any help would be appreciated!
cuteband 1-Jun-11 21:26pm    
please check the site
DanHodgson88 1-Jun-11 21:44pm    
that website doesnt exist

Share this answer
DanHodgson88 2-Jun-11 5:33am    
Thank you for these links, liek I said I am new to useing databases in c# and much appreciated your time to posts the links I have looked through them and they are/will become very useful!
thatraja 2-Jun-11 5:48am    
You welcome.
Yes, those links bunch will help you anytime, if any other things come here .... we are here to help you.
Cheers. :)
I run into this one every time I install SQL Server on a machine. The default installation has Remote Connections disabled, and this function has to be manually enabled on the host machine. The method varies depending on the version of SQL Server you're running, because the tools keep changing with each version. But I'm sure you'll have no trouble finding the right instructions using Google.

Additionally, if you're planning to dynamically locate and select a specific instance on a network, you'll want to turn on the SQL Server Browser service. That, too, is disabled by default, and without it your application won't be able to discover it.
Share this answer
DanHodgson88 2-Jun-11 5:35am    
Thank you for the reply, that is exactly what I am looking for appreciate your reply. Confused the hell out of me I didn't realise that needed to be changed I just presumed that remote connections would be enabled seems to make more sense to me! Then again presume nothing I suppose! Thanks again for the help
Try to verify the actual IP address/Computer name of the data source. If you are using SQL Server Express Edition, usually, the data source could be [IP Address]\SQLEXPRESS. Try to verify it first using SQL Server Management Studio. Also, you might want putting a user name (and password if required) on your connection string. It might be one of the reasons why you can't connect to your database.
Share this answer
DanHodgson88 1-Jun-11 21:49pm    
cheers for the reply just looked at that and makes no difference still get the same error havn't got a clue why!
walterhevedeich 1-Jun-11 22:01pm    
You mean Northwind database exists on your database server? Have you tried replacing your connection string with localhost on the data source? Are you using SQL Server Express? It might help if you also mention the database version that you are using. Meanwhile, try to play around your connection string, you may want to look at this for reference.
DanHodgson88 3-Jun-11 5:57am    
hey sorry for slow reply, I am using sql compact 3.5 at the moment but I am currently downloading the full sql server 2008. I am still stuck with this problem I have been through all the suggestion i.e. changing the protocols to enabled and still getting the same error. Definitely starting to frustrate me now!

The Northwind database exists as far as I know on the local machine server and also within the project itself. I am just checking out the suggested website now.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900