In my application i have two textboxes that are used to set the price of an entries for children and adults. Everytime the textboxes are changed, the event "textbox_ValueChanged" fires which executes "priceChanged()" and saves the content of my textboxes to a MySQL Database. The columns that the prices are saved to are type double. This whole thing works fine for typing integers into "adultPriceTextbox" (for example), but when trying to write a double or float into the textbox there are the following cases:
1. -> User types "5" then "," and then "5"
In this case the program crashes as soon as the second "5" is typed.
The line that crashes is cmd.ExecuteNonQuery();
which saves the value to the database (view code further below).
The error message looks like this
Data truncated for column 'adults' at row 1"
2. -> User types "5" then "." and then "5"
In this case nothing crashes, but the value being saved to the database does not contain the dot. So "5.5" would turn into "55".
This is the code that saves the values to the database. It is inside of a class called "DatabaseInterface":
public static void updatePrice(double _adults, double _children)
{
MySqlConnection cnn = OpenCnn();
MySqlCommand cmd = new MySqlCommand("UPDATE price SET adults = '" + _adults + "', children = '" + _children + "';", cnn);
cmd.ExecuteNonQuery();
}
And this is the code that executes "UpdatePrice":
private void priceChanged()
{
double adultPrice;
double childPrice;
try
{
adultPrice = Convert.ToDouble(adultPriceTextbox.Text);
}
catch
{
adultPrice = 0.0;
}
try
{
childPrice = Convert.ToDouble(childPriceTextbox.Text);
}
catch
{
childPrice = 0.0;
}
DatabaseInterface.updatePrice(adultPrice, childPrice);
}
Note that in this special case, there are two input windows. One that sets the price for children and the other one for adults. Also my region is Germany, where decimals are written with "," instead of ".". What would be the most elegant solution to achieve a Textbox where the user can type in integers and floats / doubles?
Addition: Ideas for blocking any alphabetical input into said textboxes are welcome as well, only numbers and "." / "," should be allowed.