[Solved] DBGrid - how to catch exception with date editing? lazarus / 16-Databases ---------------------------------------------------------------------- count · 2012-03-25 23:21 Hi All! I've got a strange (imho) problem: can't find the way to catch exception when tdatetime field is filled with "crap". Example project in attach - very very basic - first colomn is of integer (and there is no way to input "crap" there) nd the second - is of datetime. Try to enter "some crap" there - and exception will be raised. How to catch it? I dont want user to shut my programm pressing cancel button... May be some compilation swithes? Thanks in advance! any help apriciated! (if forum mismatched - please fix) ---------------------------------------------------------------------- bambamns · 2012-03-26 03:53 Hi, First , dealing with date can be sometimes tricky, because you will find than lot of computers have different date format (different county), and second, in my experience, it is no so wise to let user edit data direct in database thru dbgrid - it is much better if you make some fields out from dbgrid where user can put some data , and you can control them and check before you make post/commit. Using separate fields, in this case datetime picker, you will force user to choose correct date format, and also you can check it (min or max date) before you post/commit into database. ---------------------------------------------------------------------- count · 2012-03-26 10:53 Quote from: bambamns on March 26, 2012, 05:53:57 am ...in my experience, it is no so wise to let user edit data direct in database thru dbgrid - it is much better if you make some fields out from dbgrid where user can put some data , and you can control them and check before you make post/commit... actually it's not a question of visual design - it's much more simple: how to catch exception with poor data in dbgrid? ---------------------------------------------------------------------- JD · 2012-03-26 12:52 Quote from: count on March 26, 2012, 12:53:17 pmQuote from: bambamns on March 26, 2012, 05:53:57 am ...in my experience, it is no so wise to let user edit data direct in database thru dbgrid - it is much better if you make some fields out from dbgrid where user can put some data , and you can control them and check before you make post/commit... actually it's not a question of visual design - it's much more simple: how to catch exception with poor data in dbgrid? You're probably better off using the ZVDBDateTimePicker from the ZVDateTimeControls package which has built in data validation http://wiki.lazarus.freepascal.org/ZVDateTimeControls_Package. I've found that it makes it very, very difficult for the user to input "bad" dates. However, I myself believe it is not wise to let the user enter data directly into the database using a DBGrid. Like bambamns said, use normal form controls, validate the data and then post/commit it to the database. ---------------------------------------------------------------------- ludob · 2012-03-26 13:03 When you use persistent fields you can create you own OnSetText event handler for the "difficult" fields. There have full control of the conversion and how data is stored in the database. If you fancy you can even define your own custom dates such as 'today', etc. ---------------------------------------------------------------------- ludob · 2012-03-26 13:14 Another solution is to override the default exception handler: Code: [Select]   TForm1 = class(TForm)    private      Procedure MyException(Sender : TObject; E : Exception); ... procedure TForm1.MyException(Sender: TObject; E: Exception); begin   ShowMessage(E.Message); end; .... procedure TForm1.FormCreate(Sender: TObject); begin Application.OnException:=@MyException; ... This will show the message and an OK button ---------------------------------------------------------------------- howardpc · 2012-03-26 14:42 Another solution if you want to input data directly via a DBGrid is to create a DBGrid descendant that overrides the exception-catching code for all cells linked to date fields. Something like: Code: [Select]type   { TDateExceptGrid }   TDateExceptGrid=class(TDBGrid)   protected     procedure UpdateData; override;   end;   { TForm1 }   TForm1 = class(TForm)     Datasource1: TDatasource;     MemDataset1: TMemDataset;     procedure FormCreate(Sender: TObject);   private     FDateGrid: TDateExceptGrid;   public     { public declarations }   end; var   Form1: TForm1; implementation { TForm1 } procedure TForm1.FormCreate(Sender: TObject); begin  FDateGrid := TDateExceptGrid.Create(Self);  FDateGrid.Align := alClient;  FDateGrid.DataSource:= Datasource1;  FDateGrid.Parent:= Self; end; {$R *.lfm} { TDateExceptGrid } procedure TDateExceptGrid.UpdateData; var d: TDateTime; begin  if (SelectedField.DataType = ftDate) or (SelectedField.DataType = ftDateTime) then   if not TryStrToDate(Editor.Caption, d) then    ShowMessageFmt('%s is an invalid date',[Editor.Caption])  else   inherited UpdateData; end; ---------------------------------------------------------------------- count · 2012-03-26 16:19 Thank you howardpc, ludob! It seems strange but in case of global exceptioon catch (on form) everything is ok, but with tdbgreed descendant - exception is fired twice. Any way - overriding default exception handler works fine. Thanks again!