[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 441
  • Last Modified:

Another simple ADO query question..

I use an ADO query ( insert into ...) to add a record to a table, and execute using qry->ExecSQL()

If there's an index violation (duplicate key / illegal empty field etc) it throws an exception. Can't I trap that somehow? The query component has OnPostError and OnEditError but they don't get called? Or do I have to try to insert the record a different way?

0
andrewjb
Asked:
andrewjb
  • 2
2 Solutions
 
esoftbgCommented:
There is different methods to insert records
1). You have described it ....
2). Use the code construction:

try
  ADOQuery.Insert;
  ADOQuery.FieldByName('NAME').AsString := EditName.Text;
  //.........................................................
  ADOQuery.Post;
except
end;

3). Use stored procedure built in the database using TADOStoredProc: you will pass the parameters and then ADOStoredProc1.ExecProc

0
 
andrewjbAuthor Commented:
I've realised after posting that I can, of course, just try...catch around the ExecSQL. That works, but do you have an opinion as to the 'best' way to do it? Which one would trigger the OnPostError etc. events?

0
 
esoftbgCommented:
It really does not work:
ADOQuery1PostError();
I tryed to debug it, posted an illegal data for a field, but an exception raises and the debuger does not go into the OnPostError ?
I don't know why ....
0
 
kretzschmarCommented:
OnPostError triggers on a an open dataset,
but as u use execsql this and near all other events are not triggered

usual the best way would be the way u found

try
  q.execSQL;
except
  //handle your error
end;

meikl ;-)
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now