The exception means that the string you're trying to parse an integer from doesn't actually contain a valid integer.
The issue may be due to following reasons:
It may contains extra space. Use trim() function to delete unnecessary sapces.
String is not correct format means either it contains some alphanumeric, chars or special chars.
When your string should conatin only value from 0-9 except that it throw you exception.
You can use int.TryParse(). However if exception occurs you will get 0 as result.
int finalResult;
bool output;
string str = "";
output = int.TryParse(str, out finalResult);