0

I am trying to connect to a database using the DBConnection class. Everything works fine on the development pc as well as some other computers, however there are some computers which gives an Invalid Cast Exception when trying to call the DBConnection.Open() method.

I am using a DbProviderFactory and assign the DBConnection = DbFactory.CreateConnection() after setting DbFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");

So in short:

this.DbFactory = DbProviderFactories.GetFactory("System.Data.SqlClient");
this.Connection = this.DbFactory.CreateConnection();
this.Connection.Open(); // Throws the exeption

I have looked around and read that I should use Microsoft.Data.SqlClient instead, ie. this.DbFactory = DbProviderFactories.GetFactory("Microsoft.Data.SqlClient");. However when I use this and run the program I get the error - 'Unable to find the requested .Net Framework Data Provider. It may not be installed.'

jarlh
  • 42,561
  • 8
  • 45
  • 63
  • "System.Data.SqlClient" Should be a part of the .NET Framework, can you verify if it is installed in the machines were you are having the issue? or try installing the runtime for it and test it if you are unsure, you can find it here https://learn.microsoft.com/en-us/dotnet/framework/install/guide-for-developers – Armando Bracho May 06 '22 at 09:37
  • I'm not sure why you would be using a factory if you are hard-coding which factory to use. If you know that you're going to use `SqlClient` then why not just use a `SqlConnection`? If the factory to use was coming from an external source then I could understand it but it seems pointless this way. – John May 06 '22 at 09:43
  • What framework is this project targeting? Is it .NET Framework or .NET Core (.NET 5+ is .NET Core) and which version? – John May 06 '22 at 09:44
  • @ArmandoBracho I just made sure, and .NET Framework is in fact installed on the pc I'm having an issue with. .NET Framework 4.7.2 is installed. The Runtime Version of the System.Data.dll in my project is v4.0.30319. I have also tried to include a `System.Data.SqlClient` reference in my project, which did not help. – Supertime Developer May 06 '22 at 09:56
  • @John the target framework is .NET Framework 4.6.1. Also that was just sample code to clearly illustrate my problem. – Supertime Developer May 06 '22 at 09:57
  • Can you provide the full information for the original exception? An invalid cast at that point seems rather strange. – John May 06 '22 at 10:00
  • Have you tried deploying the app as a self-contained app so that it ahs all the dependencies and does not rely on the system? just to make sure it is not a system issue – Armando Bracho May 06 '22 at 10:02
  • Also, what connection string are you using? – John May 06 '22 at 10:02
  • @ArmandoBracho, can you do that with .NET Framework, or is that only an option with .NET Core? – John May 06 '22 at 10:03
  • Sorry, I am an idiot... First off I had the wrong login credentials in my ConnectionString, and secondly the SQL user didn't have access to the desired Database. Still don't know why it was an Invalid Cast Exception, but whatever, it's working now. Thanks for John and Armando trying to help though. If I could, I would downvote my own post XD. – Supertime Developer May 06 '22 at 10:37
  • You are correct this is a .NET core feature apologies, an alternative to this might be https://stackoverflow.com/questions/54719736/is-possible-to-deploy-a-self-contained-net-framework-application – Armando Bracho May 06 '22 at 10:45

0 Answers0