troubleshooting Question

SQL Server CE connection error message

Avatar of codefinger
codefingerFlag for United States of America asked on
Web Languages and StandardsMicrosoft SQL Server.NET ProgrammingC#
10 Comments1 Solution1305 ViewsLast Modified:
In app.config file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="WindowsFormsLearning_C.Properties.Settings.NorthwindConnectionString"
            connectionString="Data Source=E:\Documents and Settings\SampleDatabase\Northwind.sdf"
            providerName="Microsoft.SqlServerCe.Client.3.5" />
    </connectionStrings>
</configuration>

In button code:

// connect to a database and retrieve data:
            try
            {
                SqlConnection sqlc = new SqlConnection();
                sqlc.ConnectionString = Properties.Settings.Default.NorthwindConnectionString;
                

                sqlc.Open();   <<------Fails

                DataSet xds;
                SqlCommand xsqlcmd = new SqlCommand();
                StringBuilder sbsql = new StringBuilder();

                sbsql.Append("Select distinct ");
                sbsql.Append(" 'Company Name' ");
                sbsql.Append(" from Customers ");

                xds = new DataSet();

                SqlDataAdapter sqdap = new SqlDataAdapter(sbsql.ToString(),sqlc);

                sqdap.Fill(xds, "Customers");

                if (xds == null) { 
                
                    //do something
                }
               else {

                   foreach (DataRow xdr in xds.Tables[0].Rows)
                   {

                       //ds.Tables[0].Rows[0]["ColumnName"].ToString() 
                       Console.WriteLine(xdr["Company Name"]);
                   }

                    // do something else
                    
                    }
                sqlc.Close();
            }
            catch(Exception exc){

                MessageBox.Show(exc.Message);
                
              }
            }


Exception message (some of it, anyway):

System.Data.SqlClient.SqlException was caught
  Message=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: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)
  Source=.Net SqlClient Data Provider
  ErrorCode=-2146232060
  Class=20
  LineNumber=0
  Number=-1
  Server=""
  State=0

I know the path to the database file is correct, because I can successfully connect to it and test the connection from within the IDE connection dialog.  I copied and pasted that connection string into the config file.

So what am I doing wrong?

Thanks in advance!
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 10 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros