Reputation: 12735
I have this code here:
decimal dec = (decimal)MyDataTable.Compute("Min(Rooms)", string.Empty);
It always tells me Specified cast is not valid. How could I check if the Compute will succedd at runtime?
Thanks :)
Upvotes: 1
Views: 2491
Reputation: 33143
Try to use Decimal.TryParse()
See if that helps you.
Here is the msdn
This should work:
var s = "123.34";
decimal d;
bool isDec = Decimal.TryParse(s, out d);
if (isDec)
Console.WriteLine("It was a decimal: " + d);
else
Console.WriteLine("Not a decimal!");
Console.WriteLine(isDec);
Console.ReadLine();
The reason you need two parameters is the first parameter is a representation of the decimal in string format. If TryParse succeeds the result is stored in d
(in the above example). In this above example isDec
prints true
.
Upvotes: 2
Reputation: 30097
Try this
object dec = MyDataTable.Compute("Min(Rooms)", string.Empty);
decimal d;
bool result = Decimal.TryParse(dec.ToString(), out d);
if result
is true
it means parse was successful
Upvotes: 6