tags:

views:

85

answers:

1

Dear all, I am currently at an utter loss. There isnt a single tutorial i read online that covers raising a ValidationError on encountering a duplicate key value in a CRUD application.

Basically, I have a data entry form on which on clicking the OK button will insert a record in the database. That table has enforced a primary key constraint on one of the columns. On inserting a record with a duplicate PK value, I have written code in the catch block of the method that does the record insertion. On analyzing the error code, I am able to trap the exception raised.

Two questions though:

  1. How do i write code that actually raises a ValidationError and visually prompt the user like shading the textbox some color or something?

  2. How do i know exactly which field has raised the error? Trapping SQLErrors and checking the error code only reveals the fact that an error has occurred but it doesnt exactly tell you which field has caused the error.

Thank you very much. Really appreciate any pointers or any tutorials that cover this.

Db used is db2.

A: 

How about this?

http://joshsmithonwpf.wordpress.com/2008/11/14/using-a-viewmodel-to-provide-meaningful-validation-error-messages/

This tutorial should answer your question #1.

For your question #2, I'm not sure I understand how inserting a record with a duplicate field would be linked to any particular data entry field. In fact, shouldn't your database be assigning and incrementing the primary key value automatically? This would eliminate the possibility of a duplicate key entirely.

DanM
Hello Dan, thanks for the speedy reply. But however it does not cover exceptions raised by another class (i.e. the class that communicates with the database). I have read that article but my feeble brain is unable to retrofit the example to suit my primary key validation purpose.
cygnus atratus
A primary key value needn't be an identity field. Primary keys only enforce uniqueness on a table. An example: Social security numbers? A duplicate entry should not be allowed. So how do we raise a ValidationError on encountering that?
cygnus atratus
Okay, so are you saying that you're having the user enter some sort of identification number, and the database is using this number to determine if the user has already created an account? If this is right, highlighting the ID field (e.g., turning it red) is not really the appropriate UI to signal this type of error. I would pop up a dialog to inform the user that the account already exists. It might be nice to also give the user the option to edit his/her profile at this point. But I think this situation is beyond the standard `ValidationError` scenario.
DanM
Hi dan, yes you are spot on. Well i respectfully have to disagree that popping up a dialog box is the best way for theres an additional window and a button to be clicked making the software less userfriendly. I rather highlight the border of the textbox and display a message beside it stating the problem. Such a method will allow a consistent way of displaying validation errors.
cygnus atratus