Use the Decimal.TryParse function.
decimal value; if(Decimal.TryParse(strOrderId, out value)) // It's a decimal else // No it's not.
You can use Decimal.TryParse to check if the value can be converted to a Decimal type. You could also use Double.TryParse instead if you assign the result to a variable of type Double.
MSDN example:
string value = "1,643.57"; decimal number; if (Decimal.TryParse(value, out number)) Console.WriteLine(number); else Console.WriteLine("Unable to parse '{0}'.", value);
use Decimal.TryParse
to check if the string entered is decimal or not.
decimal d;
if(decimal.TryParse(textBox1.Text, out d))
{
//valid
}
else
{
//invalid
MessageBox.Show("Please enter a valid number");
return;
}
Conversion Conversion Emoticon Emoticon