1

I am using code like this, but I get an exception: "The underlying provider failed on open." What is the possible reason for that? I have commented the connection string in the app.config file.

SqlConnectionStringBuilder sqlConnection = new SqlConnectionStringBuilder();

sqlConnection.DataSource = dataSource;
sqlConnection.InitialCatalog = initialCatalog;
sqlConnection.IntegratedSecurity = true;

EntityConnectionStringBuilder entityConnection = new EntityConnectionStringBuilder();
entityConnection.Provider = "System.Data.SqlClient";
entityConnection.Metadata = "res://*/Restaurant.csdl|res://*/Restaurant.ssdl|res://*/Restaurant.msl";
entityConnection.ProviderConnectionString = sqlConnection.ToString();

using (EntityConnection conn = new EntityConnection(entityConnection.ToString()))
{
    conn.Open();
    MessageBox.Show("Just testing the connection.");
    conn.Close();
}
Dagg Nabbit
  • 75,346
  • 19
  • 113
  • 141
mayank.karki
  • 750
  • 1
  • 9
  • 34
  • 2
    Please post the InnerException. – SLaks Mar 16 '12 at 16:54
  • Duplicate? http://stackoverflow.com/questions/2475008/the-underlying-provider-failed-on-open – Gert Arnold Mar 16 '12 at 21:16
  • Please post the value of "entityConnection.ToString()" – usr Mar 16 '12 at 22:11
  • This exception means that your application is not able to connecto to your database. Possible reasons can be: your `sqlConnection` has invalid configuration or your database doesn't accept remote connection (if it is not on your machine) or you don't have permissions to access the database. – Ladislav Mrnka Mar 17 '12 at 11:01
  • Thanks for reply. Value is {metadata=res://*/Restaurant.csdl|res://*/Restaurant.ssdl|res://*/Restaurant.msl;provider=System.Data.SqlClient;provider connection string="Data Source=KARKI/SQLEXPRESS;Initial Catalog=Restaurant;Integrated Security=True"} – mayank.karki Mar 17 '12 at 17:21
  • If I use the manual connection that is of app.config file than it is working fine – mayank.karki Mar 17 '12 at 17:22

0 Answers0