Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keyword not supported: 'provider'. Opening SqlConnection

Tags:

c#

asp.net

I don't know why this error, I tried everything. I want to connect my webForm to the Database .accdb and when I use using(){} I got this error "Keyword not supported: 'provider" Here is the code:

web.config

<connectionStrings>
    <add name="ConnectionString"
    connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Manuel_2\Documents\Login.accdb"     
    providerName="System.Data.OleDb" />
</connectionStrings>

WebForm1

private static string conDB =        
            ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;    

protected void Page_Load(object sender, EventArgs e)
{    
   using (SqlConnection con = new SqlConnection(connDB))  //here is the error
   {
       // .....                            
   }              
}
like image 529
manuel.koliqi Avatar asked Dec 09 '14 20:12

manuel.koliqi


People also ask

How do I declare SqlConnection?

Creating a SqlConnection Object A SqlConnection is an object, just like any other C# object. Most of the time, you just declare and instantiate the SqlConnection all at the same time, as shown below: SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

What is SqlConnection in Ado net?

ADO.NET SqlConnection Class. It is used to establish an open connection to the SQL Server database. It is a sealed class so that cannot be inherited. SqlConnection class uses SqlDataAdapter and SqlCommand classes together to increase performance when connecting to a Microsoft SQL Server database.

Does using SqlConnection open connection?

The SqlConnection draws an open connection from the connection pool if one is available. Otherwise, it establishes a new connection to an instance of SQL Server. If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close.

Does using SqlConnection close connection?

The sqlConnection will close the connection after it will pass using block and call Dispose method.


1 Answers

Aleksey Mynkov has it right. But here is more detail since you are needing more clarification.

Your web.config is fine. The auto-generated Visual Studios connection string is using the right setup. Instead, on your webform1 file you need to do 2 things.

  1. Add using System.Data.OleDb.OleDbConnection; to the top of your file, and remove the using System.Data.SqlConnection;

  2. Change your webform1 code to be:

     private static string conDB = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
     protected void Page_Load(object sender, EventArgs e)
     {
         using (OleDbConnection con = new OleDbConnection(conDB))  //here is the error
         {
         }
     }
    
like image 59
JClaspill Avatar answered Sep 16 '22 12:09

JClaspill