As seen from the question the
objDFPropTemp.dfFieldType
is being tested for equality with
"money" or "decimal"
from which it can be inferred that
objDFPropTemp.dfFieldType
is of
string
type.
So the statement
(decimal)objDFPropTemp.dfFieldValue
will throw error as
string
cannot be cast to
decimal
.
Further this block of code will be executed only if
(decimal)objDFPropTemp.dfFieldValue
is either
"money" or "decimal"
in which case there is no point in converting this
string to decimal.
So I think the following statement may be used
if (objDFPropTemp.dfFieldValue != null)
txt.Text = objDFPropTemp.dfFieldValue.ToString();
instead of
else if ((objDFPropTemp.dfFieldType == "money") || (objDFPropTemp.dfFieldType == "decimal"))
{
if (objDFPropTemp.dfFieldValue != null)
{
txt.Text = ((decimal)objDFPropTemp.dfFieldValue).ToString();
}
}
else
txt.Text = (string)objDFPropTemp.dfFieldValue;
}
[Update]in response to the comment from OP
If
objDFPropTemp.dfFieldValue
holds a numeric value in
string
format and it is required to check for its value then it can be converted to
numeric value
using
Int32.TryParse
as it will not throw error if the string value is
null or not in the correct format
, and then can be checked as shown below
int val;
(Int32.TryParse(bjDFPropTemp.dfFieldValue, out val) && val > 0)