Forum > Databases

[SOLVED] DBGrid Class: EDatabaseError Mensaje: "1.650,00" is not a valid float

(1/1)

lainz:
When I'm editing on a DBGrid and I type a float with a wrong format like this one: "1.650,00" I get an exception like this:

Class: EDatabaseError
Mensaje: "1.650,00" is not a valid float

There's a way to prevent the exception? And give the user a proper error message, like: 'The number you entered has a wrong format, please try again.' or something around the lines.

Thanks.

wp:
I had thought this would be easier...

DBGrid inherits from its ancestors the event OnValidateEntry, however, it is declared only as protected and thus not accessible by the standard DBGrid. But it can easily be subclassed it to make the event public. Add the following declaration to the top of the form with the DBGrid (or to a separate unit which must be at the end of the uses line!):


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TDBGrid = class(DBGrids.TDBGrid)  public    propety OnValidateEntry;  end;
Then write a handler for this event, something like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.ValidateEntryHandler(Sender: TObject; ACol, ARow: Integer;  const OldValue: string; var NewValue: String);var  x: Double;begin  if (DBGrid1.SelectedField is TFloatField) and not TryStrToFloat(NewValue, x) then  begin    MessageDlg(NewValue + ' is not a valid float.', mtError, [mbOK], 0);    Abort;  end;end;
Assign this handler to the DBGrid before its usage, e.g. in the form's OnCreate event:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.FormCreate(Sender: TObject);begin  ...  DBGrid1.OnValidateEntry := @ValidateEntryHandler; end;

korba812:
You can validate input text in TField.OnSetText event, and raise an EAbort exception if the value is invalid or just leave contents of field unchanged.

lainz:
@wp thanks, but unfortunately that code didn't work. I've tested on Lazarus 2.2.2 32 bit on Windows.

@korba812 thanks that worked!


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- ... sqlqyProductos.Fields.FieldByName('Stock_Fisico').OnSetText:=@OnSetTextStockFisico; ...  procedure TfrmInventario.OnSetTextStockFisico(Sender: TField;  const aText: string);var  o: double;begin  if not TryStrToFloat(aText, o) then  begin    MessageDlg(aText + ' NO es un nĂºmero con un formato correcto.', mtError, [mbOK], 0);    Abort;  end  else  begin    Sender.Value := o;  end;end;

wp:

--- Quote from: lainz on July 05, 2022, 02:00:19 am ---@wp thanks, but unfortunately that code didn't work. I've tested on Lazarus 2.2.2 32 bit on Windows.

--- End quote ---
Strange, I had tested it before posting, and it had worked. But anyway, korba812's solution is much better because input validation should not be performed by the visual controls but by the database itself.

Navigation

[0] Message Index

Go to full version