0

I have a C# program which uses MySqlDataReader to connect to a MySQL DB. At one point it imports a DB field of datatype double, into a C# variable also of type double:

MyDBTable table = new MyDBTable();
table.myDouble = row.GetValueOrDefault<double>("double_field");

But this results in the exception:

System.InvalidCastException: Specified cast is not valid.

Why am I getting this error? I thought that the MySQL double type corresponds to the C# double type.

user2181948
  • 1,646
  • 3
  • 33
  • 60
  • Did you try to debug the code to see which object is stored inside the column `"double_field"`? Check also [this question](https://stackoverflow.com/questions/14138013/c-problems-with-getting-double-values-from-mysql-database) – Massimiliano Kraus May 31 '17 at 23:23

1 Answers1

0

My guess is your double_field is nullable and making double value from null is not supported. try to do

object o = row.GetValueOrDefault<object>("double_field");

and look what o object will contain.

Vlad
  • 793
  • 6
  • 15