int.Parse, Input string was not in a correct format

Try this:

int number;
if (int.TryParse(TextBox1.Text, out number))
{
    //Some action if input string is correct
}

If you're looking to default to 0 on an empty textbox (and throw an exception on poorly formatted input):

int i = string.IsNullOrEmpty(Textbox1.Text) ? 0 : int.Parse(Textbox1.Text);

If you're looking to default to 0 with any poorly formatted input:

int i;
if (!int.TryParse(Textbox1.Text, out i)) i = 0;

if(!String.IsNullOrEmpty(Textbox1.text))
    var number = int.Parse(Textbox1.text);

Or even better:

int number;

int.TryParse(Textbox1.Text, out number);

Well, what do you want the result to be? If you just want to validate input, use int.TryParse instead:

int result;

if (int.TryParse(Textbox1.Text, out result)) {
    // Valid input, do something with it.
} else {
    // Not a number, do something else with it.
}

Tags:

C#