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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With