Validating whether a textbox contains only numbers

c#, exception, mysql, try-catch

Solution

It's a good idea to use the correct column datatype for what you plan to store in it, but it's very easy to check whether a string contains only numbers - just parse and see if an error is returned:

int parsedValue;
if (!int.TryParse(textBox.Text, out parsedValue))
{
    MessageBox.Show("This is a number only field");
    return;
}

// Save parsedValue into the database

Problem

So I have an idea, because i find it hard to make a code for txtbox that will only allow integer and not a letters in mysql using c#. My Plan is why not set the database column into integer instead of typical varchar and if ever you put a letter of course it will turn and exception so in that case I want to catch the exception and prompt a messagebox saying "Please enter only integer". What do you think?

Original source