You need to parse the string, and you also need to ensure that it is truly in the format of an integer.
The easiest way is this:
int parsedInt = 0;
if (int.TryParse(TextBoxD1.Text, out parsedInt))
{
// Code for if the string was valid
}
else
{
// Code for if the string was invalid
}
As explained in the TryParse documentation, TryParse() returns a boolean which indicates that a valid number was found:
bool success = Int32.TryParse(TextBoxD1.Text, out val);
if (success)
{
// put val in database
}
else
{
// handle the case that the string doesn't contain a valid number
}
You can use either,
or
You need to parse the string, and you also need to ensure that it is truly in the format of an integer.
The easiest way is this:
As explained in the TryParse documentation, TryParse() returns a boolean which indicates that a valid number was found:
Be carefull when using Convert.ToInt32() on a char!
It will return the UTF-16 Code of the character!
If you access the string only in a certain position using the
[i]
indexing operator it will return achar
and not astring
!Enjoy it...
It won't throw if the text is not numeric.