none
how to check the situation of "unique key duplicated" or "not null" in ConstraintException RRS feed

  • Question

  • hi

        I'd like to ask a question.

       I have a Dataset and in the Dataset there are contraint for "not null" in columnA and "unique key" for columnB. when there is  ConstraintException, i saw in ConstraintException.message, it is "Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints", but may I ask how could i know the detail situation? is it because of  "not null" in columnA ,or because of  "unique key" for columnB?

       thank u very much

    best regards

    • Moved by CoolDadTx Thursday, April 2, 2015 5:01 PM ADO related
    Thursday, April 2, 2015 1:44 PM

Answers

  • Why does it matter?  Your code is going to respond the same way irrelevant of the exact cause.  If you are trying to display a more meaningful message to the user then the correct approach isn't to parse the exception message but rather use data annotations on a class that can convey the actual errors.  For example if you don't want to allow a string to be null then a Required attribute on a string property will enforce that.  If you then want to use a dataset to store the data in the database then you could although at that point it seems redundant but at least the data would be valid.

    As for ConstraintException it doesn't provide any extra details beyond the message itself.  EF created a derived type to provide more info but unless you're using EF then it won't help you.  You could look at the Data property but I doubt it is set to anything useful. 

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Thursday, April 2, 2015 5:00 PM

All replies

  • Why does it matter?  Your code is going to respond the same way irrelevant of the exact cause.  If you are trying to display a more meaningful message to the user then the correct approach isn't to parse the exception message but rather use data annotations on a class that can convey the actual errors.  For example if you don't want to allow a string to be null then a Required attribute on a string property will enforce that.  If you then want to use a dataset to store the data in the database then you could although at that point it seems redundant but at least the data would be valid.

    As for ConstraintException it doesn't provide any extra details beyond the message itself.  EF created a derived type to provide more info but unless you're using EF then it won't help you.  You could look at the Data property but I doubt it is set to anything useful. 

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Thursday, April 2, 2015 5:00 PM
  • OK, thank u very much, i will try data annotations

    best regards

    martin

    Friday, April 3, 2015 9:31 AM