Casting will succeed only if the object returned by datareader["percent"] is of type Decimal . The conversion will succeed if the object is of any type convertible to Decimal . This includes int , long , short , etc. Or, in general, anything that implements IConvertible and returns a usable value from IConvertible.ToDecimal() can be passed to Convert.ToDecimal() .
For instance:
csharp> object a = (int)1; csharp> a.GetType(); System.Int32 csharp> var dec = (decimal)a; System.InvalidCastException: Cannot cast from source type to destination type. at Class3.Host (System.Object& $retval) [0x00000] in <filename unknown>:0 at Mono.CSharp.Evaluator.Evaluate (System.String input, System.Object& result, System.Boolean& result_set) [0x00000] in <filename unknown>:0 at Mono.CSharpShell.Evaluate (System.String input) [0x00000] in <filename unknown>:0 csharp> var dec = Convert.ToDecimal(a); csharp> dec; 1 csharp> dec.GetType(); System.Decimal
source share