Recent

Author Topic: Nice Dialog instead of hardcoded  (Read 4193 times)

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Nice Dialog instead of hardcoded
« on: February 14, 2017, 02:32:20 pm »
How do I tell to my users nicely : 'this person can not be deleted. There's still a relation to an subscription'? Now I get an error from lazarus/FPC itself:
Quote
An error occured while applying the updates in a record: IBConnection1 : Execute :
 -violation of FOREIGN KEY constraint "FK_INSCHRIJVINGEN_NAW" on table "INSCHRIJVINGEN"
 -Foreign key references are present for the record.

Press OK to ignore and risk data corruption.
Press Cancel to kill the program.

I'm using  SQLDB with lazarus 1.62 / FPC 3.0 and firebird
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

balazsszekely

  • Guest
Re: Nice Dialog instead of hardcoded
« Reply #1 on: February 14, 2017, 02:52:45 pm »
Maybe this:
  Cannot delete "John Smith". The user still has one or more active subscription,  please delete the active subscriptions first.
In the same dialog I would show the active subscriptions and a button to delete them.
Another solution is to offer the possibility to delete the subscriptions:
  The user "John Smith" has one or more active subscription. Do you wish to delete the active subscriptions as well?
« Last Edit: February 14, 2017, 03:48:06 pm by GetMem »

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1314
    • Lebeau Software
Re: Nice Dialog instead of hardcoded
« Reply #2 on: February 15, 2017, 02:23:14 am »
How do I tell to my users nicely : 'this person can not be deleted. There's still a relation to an subscription'?

Sounds like your DB action is raising an exception.  Simply catch the error in a 'try/except' block, parse it to figure out what it is trying to say, and then display a friendlier message to you user.  What's so hard about that?
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Nice Dialog instead of hardcoded
« Reply #3 on: February 15, 2017, 03:04:53 am »
I'm using  SQLDB with lazarus 1.62 / FPC 3.0 and firebird

You might have a different approach using Lazarus or Firebird (via Stored Procedure):
- before delete master record, check its dependencies;
- if there is no dependencies, then delete master record and inform your user that delete was successful;
- or inform you user that delete master record is not possible due dependencies.

Some initial information about how to create Stored Procedures in Firebird:
https://firebirdsql.org/refdocs/langrefupd21-ddl-procedure.html
https://firebirdsql.org/refdocs/langrefupd25-ddl-procedure.html

JD

  • Hero Member
  • *****
  • Posts: 1848
Re: Nice Dialog instead of hardcoded
« Reply #4 on: February 18, 2017, 12:44:46 am »
The origin of the exception is violation of a foreign key constraint. Some databases have error codes and you can trap these error codes or key text like violation of foreign key constraints in a try except block like Rémy suggested.

Use something like AnsiContainsStr to test for the presence of the error code or key text and then display a more user friendly message to the user. That is how I did it in one of my apps.

Just by way of example, I have an application server working with a PostgreSQL database. Exceptions are caught server side and sent to clients as-is! On the client side I parse the exception message for codes and/or certain key text and display a friendly message like in the example below
Code: Pascal  [Select][+][-]
  1.  // '23505' is an SQL State error for duplicate keys
  2. if AnsiContainsText(strServerResponse, '23505') then  
  3.      MessageDlg(Format(strUserExistsAlready, [Trim(ledtFirstName.Text) + ' ' + Trim(ledtLastName.Text)]), mtWarning, [mbOK], 0);
  4.  
« Last Edit: February 18, 2017, 01:02:16 am by JD »
Windows - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe),
Linux Mint - Lazarus 2.1/FPC 3.2 (built using fpcupdeluxe)

mORMot; Zeos 8; SQLite, PostgreSQL & MariaDB; VirtualTreeView

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
Re: Nice Dialog instead of hardcoded
« Reply #5 on: February 18, 2017, 08:11:48 am »
It's a combination of SQLdb and database.
SQLdb is related in two parts: internal dataset (TBufdataset) and external database. In the moment the record is successfully changed in TBufdataset, events like OnDeleteerror, OnInsertError, OnPostError doesn't work anymore. The only relation to TBufDatabase and database itself is OnUpdateError.

In Theory the OnDeleteError must be triggered, so the developer can input his on message and the record in TBufdataset must be rollbacked.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

 

TinyPortal © 2005-2018