开发者

WPF Validation of Primary Key

开发者 https://www.devze.com 2022-12-14 13:13 出处:网络
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.

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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消