21

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
   {
       // .....                            
   }              
}
4
  • Is it a typo or do you have really that > between Data and Source?
    – Steve
    Commented Dec 9, 2014 at 20:12
  • 7
    You can't use an OleDB connection string with SqlConnection.
    – SLaks
    Commented Dec 9, 2014 at 20:14
  • @steve it's a bad type, I saw a video of kudvenkat that uses that Commented Dec 9, 2014 at 20:16
  • @manuel.koliqi you have your answer. The comment from Slaks is right
    – Steve
    Commented Dec 9, 2014 at 20:17

5 Answers 5

29

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
         {
         }
     }
    
3
  • 1
    does not recognise the using OleDB Commented Dec 9, 2014 at 20:33
  • 1
    now it's working I used only using System.Data.OleDb and changed the SqlConnection. Thank you all Commented Dec 9, 2014 at 21:08
  • 1
    @manuel.koliqi Don't forget to choose an accepted answer and up votes on all your solved questions. Welcome to Stack Exchange.
    – JClaspill
    Commented Dec 9, 2014 at 22:52
12

I know this is somewhat an old thread and already answered but i'm adding my solution for future reference

I have SQL server 11.0 database, and i encountered the error when i was trying to work with it in SharePoint app, I haven't tried the other proposed answers, but i simply just deleted the "Provider" portion (and reordered) , so my connection string which looked like this:

Provider=SQLOLEDB.1;Password=DBPassword;Persist Security Info=True;User ID=sa;Initial Catalog=DBName;Data Source=DBServer

Now looks like this:

Data Source=DBServer;Initial Catalog=DBName;Persist Security Info=True;User ID=sa;Password=DBPassword;

And it worked just fine

2
  • Thank you, I just deleted the Provider bit and it worked fine
    – majjam
    Commented Feb 26, 2016 at 10:08
  • you my friend are most welcome, glad to know i helped Commented Aug 15, 2016 at 11:53
5

You should use System.Data.OleDb.OleDbConnection.

3
  • the <connectionStrings> generated visual studio itself when I added the source on the gridview Commented Dec 9, 2014 at 20:18
  • @manuel.koliqi Change the using statement to use OleDbConnection is what everyone is talking about. Not the connection string.
    – JClaspill
    Commented Dec 9, 2014 at 20:19
  • does not recognise it, it says is a type not a namespace Commented Dec 9, 2014 at 20:23
1

I found the error in question here (and similar) while working in Visual Studio and passing the Connection Manager name as a parameter to the Script Task to determine its connection string. Using the ConnectionString method brings a connection string with more elements (or value/pairs) than expected (including provider). The expected connection string, in my case, required only Data Source, Initial Catalog, and Integrated Security.

There are two options I found to solve this issue. The first, which didn't really work for me but hope it works for you, was to do the following:

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(Dts.Connections[connMgrName].ConnectionString);
string connectionString = builder.DataSource + ";" + builder.InitialCatalog + ";" + builder.IntegratedSecurity;

I hope the above works for you because you solve the issue in a couple lines of code. However, the option that worked for me was to recreate my connection string by only selecting the value/pairs that the database needed:

string connectionString = Dts.Connections[connMgrName].ConnectionString;    //  ConnectionString will contain unsupported keywords like 'provider'
connectionString = connectionString.Trim(';'); //  Remove the trailing semicolon so that when we perform the split in the following line, there are no index errors.
var connStrDictionary = connectionString.Split(';').Select(x => x.Split('=')).ToDictionary(x => x[0], x => x[1]);     //   Here we get each value-pair from connection string by splitting by ';', then splitting each element by '=' and adding the pair to a Dictionary.
try
{
    connectionString = "Data Source=" + connStrDictionary["Data Source"] + ";Initial Catalog=" + connStrDictionary["Initial Catalog"] + ";Integrated Security=" + connStrDictionary["Integrated Security"]; //  Build the actual connection string to be used.
}
catch(KeyNotFoundException)
{
    Console.WriteLine("\t\tNot able to build the connection string due to invalid keyword used. Existing keywords and their values:");
    foreach( KeyValuePair<string, string> kvp in connStrDictionary)
    {
        Console.WriteLine("\t\t\tKey = '{0}', Value = '{1}'", kvp.Key, kvp.Value);
    }
}

Hope it helps. Good luck!

0

OK, I know this is pretty old, and I know that the answer was right in front of my face the whole time, but I'd like to emphasize that the one parameter that was messing me up because it was missing wasproviderName="System.Data.OleDb". Just in case anyone else looking at this is as obtuse as I am.

1
  • Where did you add that? Is that "parameter" you mentioned really an attribute of a connection string element in a config file? Instead of being coded in the connection string itself?
    – Suncat2000
    Commented Sep 4 at 20:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.