Talha
Talha

Reputation: 911

Connection Error in SQL Server Compact with ASP.Net

I am trying to access a database, using a DataSet. I am using Visual Studio for Web Express 2013, ASP Web Forms project with SQL Server Compact 4.0. I created the database from Database Explorer, added 3 tables, now my code has getData, setData methods which will use Dataset to get data.

But the SqlConnection object is throwing exception at runtime.

[SqlException (0x80131904): 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)]

I guess this is due to connection string since error is at line:

SLQCon.Open();

I have tried more than one method but none of them has worked yet. These are few of them,

msdn link

Connection Strings

The other things work fine (web form controls), only problem is when I do something related to database, any fixes?

My code is as follows:

void setData(string tableName) {   
    string ConString = @"Data Source=" + "F:\\Github\\JournalClassifier\\WebApplication1\\WebApplication1\\App_Data\\KeywordsDB.sdf" + ";Connect Timeout=30";

    SQLCon.ConnectionString = ConString; 

    SQLCon.Open();      // Exception here

    // insertion code  
}

Upvotes: 0

Views: 397

Answers (1)

vantian
vantian

Reputation: 888

Make sure your database inside App_Data folder on your root application. and change the connection string to this one

<add name="ConnectionStringName"
    providerName="System.Data.SqlServerCe.4.0"
    connectionString="Data Source=\KeywordsDB.sdf;Connection Timeout=30" />

things you need to concern is you are using SQL Server Compact, so the provider name need use System.Data.SqlServerCe.4.0 otherwise it will confused with SQL Server Client

Upvotes: 1

Related Questions