Catching database exceptions

I have TTable component with persistent fields pointing to a local table (FoxPro). Further, I have form on which user enters data to my table. I need to catch exceptions which occur when end-user enters invalid value into data-aware component (or such value is assigned to a field programatically in code), for example a string value into TDBEdit that points to TDateField. Such exceptions are raised before actual posting to table, therefore they don't trigger OnPostError event.

Thanks for Help, Ivo.
LVL 2
ivobauerAsked:
Who is Participating?
 
kretzschmarConnect With a Mentor Commented:
hi ivobauer

samples alternative

procedure TForm1.Table1BirthdayValidate(Sender: TField);
begin
  if Sender.AsDateTime > Now then
    Raise Exception.Create('Error! The Birthday is in the future!');
end;

procedure TForm1.Table1BirthdaySetText(Sender: TField; const Text: String);
begin
  Try
    Sender.AsDateTime := StrToDate(Text);
  except
    raise Exception.Create('There is a problem with the Date-Input!');
  end;
end;

meikl
0
 
kretzschmarCommented:
hi ivo,

each tfield has an onvalidate-event, where you canb place your own validation. this event is fired before the vcl tries to place the value into the current record-buffer.

maybe thats a point for you to catch a possible exception before one is raised automatically.

also you can use the onsettext-event of the tfield, by trying to set the value byself into the recordbuffer, and raising an exception byself, if the try failed.

tell me, if you need a sample

meikl
0
 
ivobauerAuthor Commented:
I'll be very appreciated if you send me some code sample.
0
 
ivobauerAuthor Commented:
Posting the points to you. Thanks!
0
 
kretzschmarCommented:
:-) thanks, hope it helps, good luck again
0
All Courses

From novice to tech pro — start learning today.