none
Exception not Raised on Duplicate Entries (Constraints Enforced and Columns Unique RRS feed

  • Question

  • My dataset does not throw an exception when duplicate entries are added to a table. The dataset is set to enforce constraints and the columns in question are set to unique.

    This happens regardless of whether .BeginLoadData is called.

                                //Add to MemberWords
                                DataRow dr3 = dataTableMemberWords.NewRow();
                                dr3 = dataTableMemberWords.NewRow();
                                dr3["Lexicon"] = idLexicon;
                                dr3["Memberword"] = wordToAdd;
                                dr3["Family"] = headword;
                                dataTableMemberWords.Rows.Add(dr3);

    The "Memberword" column is set to unique, but duplicates do not throw an exception when added. 

    Thursday, October 4, 2012 3:23 PM

Answers

  • Thanks Bonnie. I found the problem.

    There is a bug in the designer. Although it shows the enforce constraints as "True," in fact it does nothing. 

    After setting the flag in code (dataSet.EnforceConstraints = true), constraints are now enforced. 

    I am using VS2010.


    • Marked as answer by A.Russell Friday, October 5, 2012 5:57 AM
    Friday, October 5, 2012 5:57 AM

All replies

  • Are you sure you haven't turned off the constraints prior to running this code? It sure should throw an exception!! The other possibility is that you've got a try/catch somewhere that's eating exceptions ...

    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Friday, October 5, 2012 1:16 AM
  • Thanks Bonnie. I found the problem.

    There is a bug in the designer. Although it shows the enforce constraints as "True," in fact it does nothing. 

    After setting the flag in code (dataSet.EnforceConstraints = true), constraints are now enforced. 

    I am using VS2010.


    • Marked as answer by A.Russell Friday, October 5, 2012 5:57 AM
    Friday, October 5, 2012 5:57 AM
  • I don't think there's a bug in the DataSet Designer. I also use VS2010.

    I don't normally use the Designer (I just work with the xml directly in the .xsd), because I don't use (and don't like) the TableAdapter stuff. But, I  have a little test project I use for experimenting with stuff that I don't use in my own real apps. So, I took a look at one of my test DataSets that does use TableAdapters, and went to the Designer and played with the EnforceConstraints property. The setting of true (or false), is properly carried forward into the code. All I did was set a debug breakpoint, instantiate the new MyDataSet(), then looked at ds.EnforceContraints while in the debugger. It was properly set every time. So, I still think that you had turned it off somewhere and forgot to turn it back on.


    ~~Bonnie Berent DeWitt [C# MVP]

    geek-goddess-bonnie.blogspot.com

    Saturday, October 6, 2012 4:16 PM