In answer to your question, "is there a better way to do this"?
Yes, and it's called TryParse
:
int iD = 0;
if(Int32.TryParse(d.CommandArgument.ToString(), out iD))
{
// Do something with iD
}
Explanation
You're implying (using a basic Convert
method) that the input can be converted to an Int32
, which is why you get an exception when it can't. With the way I've suggested above, you're simply trying to parse it. TryParse
returns a boolean
you can then evaluate to decide on a path of execution.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…