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?
Visual studio has built in support for this (and you may also achieve it via coding). Here's what you need to do:
TextBox
.ErrorMessage
and write "Alert: Type only Number".Also via coding you can get it as:
protected void Button1_Click(object sender, EventArgs e)
{
int i;
if (!int.TryParse(textBox.Text, out i))
{
Label.Text = "This is a number only field";
return;
}
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With