Domanda

I have an application that works with a database that uses foreign key constraints.

For example, we have an inventory table that has a foreign key constraint that references a table called case_names. If a user attempts to delete a case name that has any inventory items referencing it's ID, the action is restricted.

How can I display a message to the user that they will understand? 'You can not delete this case as it still contains inventory items.'

My thought is a try-catch (catching different error types?) but I do not really want to use this for something that may be expected.

The application is in VB.Net using MySQL.data

È stato utile?

Soluzione

I wouldn't use exceptions to handle user errors or normal program flow. Just check if the user can delete the records (using maybe a stored procedure), and if not show her an alert message.

I think exceptions are for errors you can't foresee.

edit to clarify:

The stored procedure (or the code in your program) can do the checks on all the involved tables or it can even COUNT() how many records would be affected by the deletion request. By counting you have the added benefit of telling the user how many inventory items are connected with the case, in you example:

"You can not delete this case as it still contains 32 inventory items."

edit to clarify more:

This isn't to say that reference integrity shouldn't be properly set up in the DB, of course. If you forget some checks still you have the DB that works for your (and the user will call you to know what the heck the program is saying :) ).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top