In Asp.net using c# ,I am having
int sum = Convert.ToInt32(txtbox1.Text) + Convert.ToInt32(txtbox2.Text);
When i have an empty textbox, i get the error “Input string is not in a correct format”.
So, I am wondering if there is a simple way to add textbox values only they are integers, if not take the textbox value as 0.
Thanks in advance!
You can either set the default value of the
TextBoxto0, or useTryParse:Note that you do not need to set the values to 0 if the parse fails since they will be 0 anyways. If you want to be explicit, however: