Failed to find or load the registered .Net Framework Data Provider

When using a 3rd party .Net data provider. Or when you dynamically load a .Net provider you may get the following error:

 

Failed to find or load the registered .Net Framework Data Provider

 

There does not seem to be that much information about this and this post is not intended to resolve
all possible errors, more to give a hint on what to look for when this happens.

Let's start with a code example that dynamically loads the SqlClient Data Provider

    class Program

    {

        static void Main(string[] args)

        {

            try

            {

                DataTable dt = DbProviderFactories.GetFactoryClasses();

                // Use this for loop to see what row holds System.Data.SqlClient

                // "DbProviderFactories.GetFactoryClasses Method"

                // https://msdn2.microsoft.com/en-us/library/system.data.common.dbproviderfactories.getfactoryclasses(VS.80).aspx

                for (int i = 0; i < dt.Rows.Count; i++)

                    Console.WriteLine("{0}: {1}", i.ToString(), dt.Rows[i][2].ToString());

                Console.WriteLine("------------------------------------------------------\n");

 

                // For me, System.Data.SqlClient is at row 3

    DbProviderFactory dataFactory = DbProviderFactories.GetFactory(dt.Rows[3]);

                DbConnection con = dataFactory.CreateConnection();

                DbCommand cmd = dataFactory.CreateCommand();

 

                con.ConnectionString = @"Data Source<servername>;Initial Catalog=Northwind;Integrated Security=SSPI"

                cmd.Connection = con;

                cmd.CommandText = "SELECT * FROM Shippers";

 

                con.Open();

                while (con.State != ConnectionState.Open)

                    con.Open();

 

                DbDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())

                    Console.WriteLine("{0} : {1}", rdr[0].ToString(), rdr[1].ToString());

        con.Close();

            }

            catch (Exception ex)

            {

                Console.WriteLine(ex);

            }

            Console.WriteLine("Done");

            Console.Read();

        }

    }  

Basically this just gets a list of providers in the machine.config via the DbProviderFactories.GetFactoryClasses() classes.
This is loaded into a datatable, the code then just displays each provider so that you can select the provider to use.

This should, as long as you change the data source and make sure that you have the Northwind database on this source, return the data from the Shippers table.

 

Now, !!! --- Important --- !!!

 

1. Make a backup of C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config
2. Open machine.config.
3. Find the section that looks like this:

<system.data>

<DbProviderFactories>

<add name="Odbc Data Provider" invariant="System.Data.Odbc" description=".Net Framework Data Provider for Odbc" type="System.Data.Odbc.OdbcFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

<add name="OleDb Data Provider" invariant="System.Data.OleDb" description=".Net Framework Data Provider for OleDb" type="System.Data.OleDb.OleDbFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

<add name="OracleClient Data Provider" invariant="System.Data.OracleClient" description=".Net Framework Data Provider for Oracle" type="System.Data.OracleClient.OracleClientFactory, System.Data.OracleClient, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

<add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

<add name="SQL Server CE Data Provider" invariant="Microsoft.SqlServerCe.Client" description=".NET Framework Data Provider for Microsoft SQL Server 2005 Mobile Edition" type="Microsoft.SqlServerCe.Client.SqlCeClientFactory, Microsoft.SqlServerCe.Client, Version=9.0.242.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>

<add name="Microsoft SQL Server Compact Data Provider" invariant="System.Data.SqlServerCe.3.5" description=".NET Framework Data Provider for Microsoft SQL Server Compact" type="System.Data.SqlServerCe.SqlCeProviderFactory, System.Data.SqlServerCe, Version=3.5.0.0, Culture=neutral, PublicKeyToken=89845dcd8080cc91"/>

</DbProviderFactories>

</system.data>

4. Copy the original line for the SqlClient Provider and comment this out (again for backup reasons), so now you should have:

<add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

<!-- ORG! add name="SqlClient Data Provider" invariant="System.Data.SqlClient" description=".Net Framework Data Provider for SqlServer" type="System.Data.SqlClient.SqlClientFactory, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/-->

5. Insert a space after the type="System.Data.SqlClient.SqlClientFactory, like so:

<add name="SqlClient Data Provider" ... type="System.Data.SqlClient.SqlClientFactory <HERE>, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>

6. Save machine.config.

Rerun the code above, this should now present you with the error:

 

System.Configuration.ConfigurationErrorsException: Failed to find or load the registered .Net Framework Data Provider.
at System.Data.Common.DbProviderFactories.GetFactory(DataRow providerRow)
at DbProviderFactoryTest.Program.Main(String[] args)

 

Don't forget to reset your machine.config back to the original state.

I'm not trying to present a solution here, what I want is to illustrate potential reasons for the error.
It need not be a trailing space; it would fail if having System.Data.SqlClient.SqlClientFactoryXXX, or if it was not registered properly or missing on disk.

Basically this error does exactly what it says on the tin. If the provider is not found or registered, the error shows, and it could be that the name in machine.config
is incorrect. And since this load happens during runtime, it won't show up when building the application.