Reputation: 26518
Is there any way to convert from DB.Null to decimal in C#?
Like
Object[] returnvalue = new Object[1];
returnvalue[0] = null;
returnvalue[0] = Convert.ToDecimal(returnvalue[returnvalue.Length - 1]);
Though CLR is saying that DBNull.Value cannot be type casted. Still I want to know is there
any work around? Thanks in advance
Upvotes: 2
Views: 33631
Reputation: 1
It could looks like this:
private decimal GetDecimal(string v)
{
decimal r = 0.00M;
try {
r = decimal.Round(Convert.ToDecimal(v.Replace("%", "").Replace("zł", "").Replace(" ", "")), 2);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message.ToString());
}
return r;
}
Calling:
decimal val = GetDecimal(dataTable.Rows[i][j].ToString()),
Upvotes: 0
Reputation: 494
We use the below method to cast DBNull fields to decimal values in C#. The case where the field might be null can be catered for when giving the value of the field to the variable using a shorthand if.
cmd.Fill<Entity>(result, a => new Entity()
{
EntityProperty = (decimal)(a["FieldName"] == DBNull.Value ? null : a["FieldName"]),
});
Upvotes: 0
Reputation: 874
You can simply do this:
Object[] returnvalue = new Object[1];
returnvalue[0] = DBNull.Value; //null from database
returnvalue[0] = returnvalue[0] == DBNull.Value
? null
: (Nullable<decimal>) Convert.ToDecimal(returnvalue[returnvalue.Length - 1]);
Upvotes: 1
Reputation: 132384
How do you expect to cast null
to anything? It's null, nothing, nada...
That said, your best bet would be to check for DBNull
(or null
in the above case) and use/return/whatever default(decimal)
instead, though null
and DBNull
both have a significant semantic difference from just a default value; they are the absence of a value, so this is probably not what you want to do.
Upvotes: 7
Reputation: 2941
Would this help?
returnvalue[returnvalue.Length - 1] is DBNull ? 0 : (decimal?)returnvalue[returnvalue.Length - 1] ?? 0
Upvotes: 0
Reputation: 6348
object foo = DBNull.Value;
decimal d = 0;
d = (decimal)(foo as decimal? ?? 0); // handles dbnull fine with default 0
MessageBox.Show(d.ToString());
foo = 1234M;
d = (decimal)(foo as decimal? ?? 0); // handles dbnull fine with default 0
MessageBox.Show(d.ToString());
Upvotes: 4
Reputation: 44317
Have you considered using a nullable-decimal for your return value?
Though you'd still have to explicitly handle DB.Null, as that's different to null, you'd be able to differentiate the cases.
The code might look something like this ...
if (value == DB.Null)
return null;
else return decimal.Parse(value.ToString());
Upvotes: 3
Reputation: 1063403
There is a big difference between DBNull
and null
; your example only shows null
...
But in answer; not really... what value whould you give it?
You could test and use the default, or use Nullable<decimal>
(aka decimal?
):
With null
(not DBNull
)
decimal? valOrNull = (decimal?)returnvalue[0];
or to take DBNull
into account:
object val = returnvalue[0];
decimal? valOrNull = (val == null || val is DBNull) ? (decimal?)null
: (decimal?)val; // or convert if it is a string etc
Upvotes: 7
Reputation: 20378
If DBNull were to be converted to a Decimal, what value would you expect that decimal to have?
I think there is a Decimal.TryParse() function which would fail more gracefully.
Upvotes: 0