Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type

I'm having a problem with my code in object val = method.Invoke line. We're using this code to map the mdx fields to our entities (collection). Some data has DBNull values. So I did a research to check if the propertyType is Nullable and implemented the solution. see Nullable.GetUnderlyingType. But I'm still encountering this error. Cannot cast DBNull.Value to type 'System.Double'. Please use a nullable type.

string propertyKey = entry.Key;
PropertyInfo property = entry.Value;
Type propertyType = property.PropertyType;

propertyType = Nullable.GetUnderlyingType(propertyType) ?? propertyType;

object objectNeedingProperty = objectToPopulate;

MethodInfo method = _dataRowExtFieldMethod.MakeGenericMethod(new Type[] { propertyType });
object val = method.Invoke(row, new object[] { row, propertyKey });

property.SetValue(objectNeedingProperty, val, null);
like image 641
Chris Avatar asked Oct 18 '25 17:10

Chris


1 Answers

Your object has property of type double. Change it to double? so you can assign nulls to that property.

like image 105
trailmax Avatar answered Oct 20 '25 08:10

trailmax



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!