CodePudding user response:
A System.FormatException
happens when you want to "parse" a string to another type, like an integer, that makes a number in text be represented as a real number, in your case, you are calling Convert.ToInt32
, be sure that whatever string being passed as argument to this method, don't overflow the capabilities of the Int32
struct, or that the text does not contain letters or decimal points. If that is the case, to verify if a number is valid use int.TryParse
, it will only return true if the text is in a correct format like so:
if (int.TryParse("5", out int number))
{
}
Here, if "5" is not a number, the if block will be completely skipped.