Try casting it to a double
first. The row[denominator]
is boxed, so a straight cast to decimal
won't work.
You need to cast it to a double first as row[denominator]
is a double boxed as an object
i.e.
decimal d = (decimal)((double)row[denominator]);
A suggestion: try using Convert.ToDecimal() instead of direct casting.
I'd try
decimal d = Convert.ToDecimal(row[denominator]);
or
decimal d = 0;
if (!decimal.TryParse(row[denominator], out d))
//do something
Judging from the error message and description I assume that row[denominator] is a boxed double, so of type object. Unboxing can only be done to the correct underlying datattype, since the runtime doesn't now where to find the actual conversion operator from double to decimal (in your case it tries to find an operator which converts object to decimal, but that one is an unboxing operator and the underlying type is not decimal. So the right way should be to convert first to double and then to decimal:
decimal d = (decimal)(double)row[denominator];
Eric Lippert has blogged about exactly this in depth. I agree it's unintuitive at first, but he explains it well: Representation and Identity