If you are going to use the
Convert.ToInt32
method then you need to wrap that call in a try-catch so that you can capture and deal with that exception.
The better and more preferred ways would be to use the
Int32.Parse
or
TryParse
methods as Wes has alluded to in his comment.
The basic concept to learn here is that all user input should be treated as though it is evil. Validate the hell out of it and make sure that you can catch as many eventualities as you can and deal with them appropriately.
[Update]: The reasons for the exception to occur in the Convert.ToInt32 scenario are plenty. If the textbox value is empty or if the textbox value contains anything other than just 0 through 9 then this exception will occur. The idea of the try catch is to provide a graceful handling of the error.
int valueToUse;
try
{
valueToUse = Convert.ToInt32(GridView.Rows[e.RowIndex].Cells[0].Value);
}
catch( Excetion error )
{
valueToUse = 0;
}
The preferred way would be to use the TryParse though.
int valueToUse;
if( !Int32.TryParse( GridView.Rows[e.RowIndex].Cells[0].Value, out valueToUse ) )
{
valueToUse = 0;
}