I am trying to parse a very small number (ranges from 0 to 20). So, I know it is not the size issue. But my code still returns false all the time.
parseResult = int.TryParse(tempResult.ToString().Trim(), out Result);
Here tempResult is an object type as value can be an integer or a string. The value I am receiving is an integer (2, to be precise). And yet, parseResult is always false.
What am I missing here?
Edit: Apparently the value in the DB is saved as decimal and so actual value is 2M, instead of 2.0000. Now, how to fix this. I don't need the trailing zeros, just the value before decimal.
Edit: The value can be a string or a decimal. And I want to use just the decimal part. I can always cast to decimal directly, but that won't work if the value is a string.
Edit:
Full function:
object tempResult = bqObject.GetSingleValue(strQuery);
//get value from the system param table
if (tempResult == null)
{
tempResult = bqObject.GetSingleValue(strSysParamQuery);
}
//validate database values
if (tempResult == null)
{
throw new BaseException("DB100001");
}
else
{
parseResult = int.TryParse(tempResult.ToString().Trim(), out Result);
if (!parseResult)
{
throw new BaseException("DB100002");
}
}
First query reads a nvarchar column, while second one reads decimal column. So, I am trying to parse both to string, and then parsing to int. Problem is when the value is coming as decimal, tempResult.ToString().Trim() line converts it to 2M instead of 2.0000. That's why the function fails.
tempResultwhere it's supported to return true?Int32, why then convert it to string and callTryParseon it in the first place?