Enforce correct entry of decimal numbers

Posts   
 
    
clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 23-Aug-2010 23:30:39   

I have a database field in sql server called Amount that is decimal(11,2) so that is a number in the format 999999999.99

The Amount field is bound to textbox on a form. I want to ensure that the user enters the number correctly.

I'd like to prevent a user from entering: - too many digits after decimal point, e.g. 123.456 - too many digits before decimal point, e.g. 1234567890.00

If I can't prevent a user from typing bad data in the first place, I'd like to be able to display an error (using an ErrorProvider).

I'm assuming there must be some standard solution that programmers use to handle this.

I've been experimenting, but I haven't been successful at preventing a user from entering incorrect data in the first place. If I can't do that, then at least I'm assuming I could add some validation to the ValidateFieldValue() method of the Validator class I wrote for the entity.

clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 24-Aug-2010 00:13:37   

Here is some stuff I tried and results I noticed.

Remember the textbox is bound to a database field defined as decimal(11,2) LLBLGen will display an error if I enter more than 11 digits. That's good.

I noticed if I enter 1.234 and tab off the textbox, the textbox still says 1.234 yet the entity field is truncated to 1.23. I don't like the fact that what the user sees on screen doesn't match what is actually going to be saved.

I tried using advanced data binding on the property sheet of the textbox. I decided to use a numeric format with 2 decimal places.

If I enter 123.456 and tab off, it the entity's field says 123.45 but the textbox says 123.46.
I don't like that because a rounded value is displayed on screen while the truncated value is the one that is going to be saved.

If I enter 12345678901, which is too many digits before the decimal point, LLBLGen does not complain when I tab off the textbox. The entity field's value is 12345678901, but the textbox says 12,345,678,901.00. When I try to save the entity, LLBLGen says that "12345678901.00" is out of range.

I forgot the details, but when I tried using a masked textbox, the amount field of existing records would not be displayed properly in the masked textbox. Perhaps I was using the wrong mask, I used 00000000.00

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Aug-2010 06:46:12   

You can define a Scale overflow correction action. In your case, maybe None, that will throw and exception you can catch and inform the user (or clean the textbox control).

David Elizondo | LLBLGen Support Team
clint
User
Posts: 150
Joined: 15-Nov-2005
# Posted on: 24-Aug-2010 18:24:19   

daelmo wrote:

You can define a Scale overflow correction action. In your case, maybe None, that will throw and exception you can catch and inform the user (or clean the textbox control).

Thanks!