c# - Oracle.DataAccess DataRow.Field<decimal> InvalidCastException -
in code using system.data.oracleclient
ora database connection. replace library (because obsolete) oracle.dataaccess
. unfortunately found datarow.field() throws invalidcastexception
. same behavior (decimal)x.rows[0]["colume_name"]
. not have issue system.data.oracleclient
.
here code example
using (var oracleconnection = new oracleconnection(connectionstring)) { using (var command = new oraclecommand("select * tr", oracleconnection)) { var result = new datatable(); var adapter = new oracledataadapter(command); adapter.fill(result); console.writeline(result.rows[0].field<decimal>("tr_seq_num")); //console.writeline((decimal)result.rows[0]["tr_seq_num"]); } }
tr_seq_num has number(8,0)
datatype , full exception is:
system.invalidcastexception: specified cast not valid. @ system.data.datarowextensions.unboxt`1.valuefield(object value)
code example working system.data.oracleclient
not oracle.dataaccess
i know can use convert.changetype
wonder if there way have same behavior system.data.oracleclient
. refactoring of of code time expensive.
the value in database not decimal , boxed int (the 'value' parameter in error message) cannot cast decimal, despite casting int decimal being ok. this answer leads more info.
you can see in action this:
void main() { int anint = 5; object boxedint = (object)anint; decimal unboxed = convert.todecimal(boxedint); //5 decimal unboxed2 = (decimal)boxedint; //invalidcastexception }
if @ unbox.valuefield method, does...
private static t valuefield(object value) { if (dbnull.value == value) throw datasetutil.invalidcast(strings.datasetlinq_nonnullablecast((object) typeof (t).tostring())); return (t) value; }
basically you'll need to
convert.todecimal(rows[0]["tr_seq_num"]);
Comments
Post a Comment