Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
hello everyone!!

actually i am creating windows form base application, here i am using a database .sdf file, i have used app.config file for connection string, the connection is working through the dataset.xsd file, but when i using connection programmatically, its showing error while opening connection.

Error : 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)


app.config :
XML
<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="ConString" connectionString="Data Source=|DataDirectory|\Northwind.sdf" providerName="Microsoft.SqlServerCe.Client.3.5" />
    </connectionStrings>
</configuration>

c# Code
C#
string conStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            SqlCommand cmd = new SqlCommand();
            SqlDataReader dr;

            try
            {
                //open connection
                con.Open();
                //prepare connection object to get the data through
                //reader and populate into dataset
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                cmd.CommandText = "Select ProductName From Products";

Friends please suggest Something....
Posted
Updated 26-Feb-15 1:52am
v2

Hi Altaf, I had the same problem this helped me try this.
change your code to the following, you should be using "SqlCeConnection class", rather than "SqlConnection class"...
C#
string conStr = ConfigurationManager.ConnectionStrings["ConString"].ConnectionString;
            SqlCeConnection con = new SqlCeConnection(conStr);
            SqlCeCommand cmd = new SqlCeCommand();
            SqlCeDataReader dr;
 
            try
            {
                //open connection
                con.Open();
                //prepare connection object to get the data through
                //reader and populate into dataset
                cmd.CommandType = CommandType.Text;
                cmd.Connection = con;
                cmd.CommandText = "Select ProductName From Products";
 
Share this answer
 
Comments
Altaf Ansari89 11-Mar-15 0:05am    
Thank You Udoyen...
Altaf Ansari89 11-Mar-15 0:28am    
Connection is working fine but now its showing another error : SQL Server Compact does not support calls to HasRows property if the underlying cursor is not scrollable.
udoyen 11-Mar-15 7:37am    
Hi Altaf please kindly provide more details..."show the lower side of the try block. A solution I found states that you should use a while loop with the datareader.
<pre>bool hasRow = DR.Read();
if(hasRow)
{
//TODO
}</pre>

Also check out this article at MSDN (https://msdn.microsoft.com/en-us/library/ms172375.aspx). This I think would make the whole concept a bit clearer.
 
Share this answer
 

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