Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not unbox bigint to long

First of all pardon me for my english. I am getting this error__ When i try to explicitly convert object from executescalar() to long ,it gives can not unbox executescalar() to long .

while gives no error where converting to System.Decimal type.

long? id = (long?)ppwDb.ExecuteScalar(ppwDbCmd); //RAISE eXCEPTION
decimal? id = (decimal?)ppwDb.ExecuteScalar(ppwDbCmd);***// NO eXCEPTION

AND value of id is (id = 9874563) ,means under long range.

like image 878
Chandrasen Singh Avatar asked Sep 16 '25 18:09

Chandrasen Singh


1 Answers

You have to cast twice:

long? id = (long?) (decimal?) ppwDb.ExecuteScalar(ppwDbCmd);

The first cast will unbox the decimal?. The second cast will do a conversion from decimal? to long?. You cannot skip the first cast because a decimal? cannot be unboxed to a long?.

This assumes that ppwDb.ExecuteScalar(ppwDbCmd) returns object but the actual value returned is a decimal?.

However, I suspect that ppwDb.ExecuteScalar(ppwDbCmd) returns DBNull.Value if the id is null. In that case you will have to do special handling for that value:

object result = ppwDb.ExecuteScalar(ppwDbCmd);
long? id = result != DBNull.Value ? (long?) (decimal) result : null;
like image 52
Martin Liversage Avatar answered Sep 18 '25 09:09

Martin Liversage