none
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

    Question

  • Hi guys I get this error message when trying to add a row in my dataset....I create the dataset this way:


     DataSet ds = new DataSet("notater");



                DataTable dst;
               dst = ds.Tables.Add("notat");
                dst.Columns.Add("tekst");
                dst.Columns.Add("tid");
                dst.Columns.Add("name");
                dst.PrimaryKey = new DataColumn[] { dst.Columns["name"] };
                dst.Columns["name"].Unique = true;



    and write to it this way 

     DataTable dst = ds.Tables["notat"];
                   
                    
                    {
    
                        DataRow nrad = ds.Tables["notat"].NewRow();
                        nrad["tekst"] = txtNotat.Text;
                        nrad["name"] = txtName.Text;
                     
                        ds.Tables["notat"].Rows.Add(nrad);
                        ds.WriteXml("notat.xml", XmlWriteMode.WriteSchema);
                        ds.ReadXml("notat.xml"XmlReadMode.ReadSchema);
    This is the xml file: 
    <?xml version="1.0" standalone="yes"?>
    <notater>
      <xs:schema id="notater" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="notater" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="notat">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="tekst" type="xs:string" minOccurs="0" />
                    <xs:element name="tid" type="xs:string" minOccurs="0" />
                    <xs:element name="name" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
              <xs:element name="kontakt">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="navn" type="xs:string" minOccurs="0" />
                    <xs:element name="adresse" type="xs:string" minOccurs="0" />
                    <xs:element name="telefon" type="xs:string" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:choice>
          </xs:complexType>
          <xs:unique name="Constraint1" msdata:PrimaryKey="true">
            <xs:selector xpath=".//notat" />
            <xs:field xpath="name" />
          </xs:unique>
          <xs:unique name="kontakt_Constraint1" msdata:ConstraintName="Constraint1" msdata:PrimaryKey="true">
            <xs:selector xpath=".//kontakt" />
            <xs:field xpath="telefon" />
          </xs:unique>
        </xs:element>
      </xs:schema>
      <notat>
        <tekst>Veien 3</tekst>
        <tid>99999999</tid>
        <name>Ulf</name>
      </notat>
      <notat>
        <tekst>adsw</tekst>
        <name>asd</name>
      </notat>
      <notat>
        <tekst>a</tekst>
        <name>a</name>
      </notat>
      <notat>
        <tekst>zeze</tekst>
        <name>zeze</name>
      </notat>
      <kontakt>
        <navn>Ulf</navn>
        <adresse>Veien 3</adresse>
        <telefon>99999999</telefon>
      </kontakt>
    </notater>


              
    • Edited by miniHessel Thursday, March 15, 2012 12:39 PM
    • Moved by Leo Liu - MSFT Monday, March 19, 2012 5:37 AM Moved for better support. (From:Visual C# General)
    Thursday, March 15, 2012 12:27 PM

Answers

All replies

  • This is from help file :

    Relax or turn off constraints in your DataSets.

    You can use the EnforceConstraints property to temporarily turn off constraints while filling tables in a DataSet object.

    Cheers,

    Thursday, March 15, 2012 12:47 PM
  • If i try that I get:

    Error 1 'System.Data.DataTable' does not contain a definition for 'EnforceConstraints' and no extension method 'EnforceConstraints' accepting a first argument of type 'System.Data.DataTable' could be found (are you missing a using directive or an assembly reference?)

    Thursday, March 15, 2012 12:49 PM
  • I think I found your problem. I think the problem is that you load the data and you merge it with your current data. What you need to do is to just clear the datatable/dataset before loading it.

          
                ds.WriteXml("notat2.xml", XmlWriteMode.WriteSchema);
                ds.Tables[0].Rows.Clear();
                ds.ReadXml("notat2.xml", XmlReadMode.ReadSchema);

    I hope it helps.

    Thursday, March 15, 2012 12:54 PM
  • I think there is something wrong in my code..It saves everything that is in the XML file another time..so if there is


    2

    it saves it
    1
    2
    and new 3
    Thursday, March 15, 2012 1:31 PM
  • I have tried this code and it works for multiple times as well !

                DataSet ds = new DataSet();
                DataTable dst;
                dst = ds.Tables.Add("notat");
                dst.Columns.Add("tekst");
                dst.Columns.Add("tid");
                dst.Columns.Add("name");
                dst.PrimaryKey = new DataColumn[] { dst.Columns["name"] };
                dst.Columns["name"].Unique = true;
    
    
    
                DataRow nrad = ds.Tables["notat"].NewRow();
                nrad["tekst"] = "First";
                nrad["name"] = "456";
    
                nrad = ds.Tables["notat"].NewRow();
                nrad["tekst"] = "Second";
                nrad["name"] = "456";
    
                ds.Tables["notat"].Rows.Add(nrad);
                ds.WriteXml("notat2.xml", XmlWriteMode.WriteSchema);
                ds.Clear();
                ds.ReadXml("notat2.xml", XmlReadMode.ReadSchema);
    
    
                MessageBox.Show("OK!");

    Thursday, March 15, 2012 1:37 PM
  •   <notat>
        <tekst>{\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\f0\fs17 qwe\par
    }
    </tekst>
        <name>1</name>
      </notat>
      <notat>
        <tekst>qwe</tekst>
        <name>qwe</name>
      </notat>
      <notat>
        <tekst>qwe</tekst>
        <name>blabla</name>
      </notat>
      <notat>
        <tekst>qwe</tekst>
        <name>qwr</name>
      </notat>
      <notat>
        <tekst>qwe1</tekst>
        <name>1</name>
      </notat>
    </notater>



    The last one  I added is 1, this is my problem..Attached xml file 

    I attach my code too..

        public partial class Form1 : Form
        {
            DataSet ds;
           
            BindingSource bloggBindingSource;
            string clipboard; 
    
            public Form1()
            {
                InitializeComponent();
                
    
            }
    
             private void readFromKontaktTable()
            {
                DataSet ds = new DataSet();
                ds.ReadXml("notat.xml");
    
                    listBox1.DataSource = ds.Tables["kontakt"];
                    listBox1.DisplayMember = "navn";
                    listBox1.ValueMember = "telefon";
           
                                    
                      
            }
            Int64 i = 0;
            string currentNotatNr ;
    
            
           
            private void Form1_Load(object sender, EventArgs e)
            {   
                
    
    
                tabPage1.Text = @"POST-IT";
                tabPage2.Text = @"KONTAKTER";
    
                if (File.Exists("notat.xml"))
                {
                    ds = new DataSet("notater");
                    ds.ReadXml("notat.xml");
                    bloggBindingSource = new BindingSource(ds, "notat");
                    txtNotat.DataBindings.Add("Rtf", bloggBindingSource, "tekst", true);
                    txtName.DataBindings.Add("text", bloggBindingSource, "name", true);
    
                    bloggBindingSource.AllowNew = false;
                    readFromKontaktTable();
                }
    
                else
                {
    
                    createDataSetandXML(); //CREATE THE DATASET WITH TABLES ETC AND CREATE THE XML
                    dataBindings();
                    readFromKontaktTable();
                  
                }
             
                
            }
    
            private void dataBindings()
            {
                ds = new DataSet("notater");
                ds.ReadXml("notat.xml");
                bloggBindingSource = new BindingSource(ds, "notat");
                txtNotat.DataBindings.Add("Rtf", bloggBindingSource, "tekst", true);
                txtName.DataBindings.Add("text", bloggBindingSource, "name", true);
                bloggBindingSource.AllowNew = false;
            }
    
             static void createDataSetandXML()
            {
                DataSet ds = new DataSet("notater");
                // Lager tabellen person
    
                DataTable dst;
    
            
                dst = ds.Tables.Add("notat");
                dst.Columns.Add("tekst");
                dst.Columns.Add("tid");
                dst.Columns.Add("name");
                dst.PrimaryKey = new DataColumn[] { dst.Columns["name"] };
                dst.Columns["name"].Unique = true;
    
           
    
    
    
                //mulighet for nullverdier
        
                dst.AcceptChanges();
    
    
    
    
    
                ds.WriteXml("notat.xml", XmlWriteMode.WriteSchema);
    
    
    
    
           
             
               
    
            }
    
            
    // ______________________________________________________NOTAT FUNKSJONALITET_____________________________________________
    
          
    
    
    
             private void picSlettNotat_Click(object sender, EventArgs e)
             {
                   try
                    {   // FOR Å SLETTE EKSISTERENDE NOTAT
                        //THIS IS FOR UPDATING THE XML FILE !
                        DataSet ds = new DataSet();
                        ds.ReadXml("notat.xml");
                        // Skal sette inn en rad på angitt posisjon
                        DataTable dst = ds.Tables["notat"];
                        DataRow[] rows = dst.Select("navn ='" +txtName.Text+"'");  //using the integer I for the current "notat" you are browsning
                        DataRow row = rows[0];
                        row.Delete();
                       
                        ds.WriteXml("notat.xml", XmlWriteMode.WriteSchema);
                        ds.ReadXml("notat.xml");
                        bloggBindingSource.MovePrevious();
                   
                        MessageBox.Show("Notat med navn : " + txtName.Text + ", er slettet. Laster neste notat");
                     
    
                      
                       
    
                   }
    
                       
                   
    
                       
                    
    
                    catch (Exception)
                    {
                        MessageBox.Show("Du har ikke åpnet noe notat, trykk neste nedenfor"); 
                     
                    }
                
             }
    
    
             private void picLagreEksisterende_Click(object sender, EventArgs e)
            {
                // LAGRE
    
                if ((txtNotat.Text.Length > 0) && (txtName.Text.Length > 0))
                {
              
                   
                    
                    {
                        DataRow nrad = ds.Tables["notat"].NewRow();
    
    
                        nrad["tekst"] = txtNotat.Text;
                        nrad["name"] = txtName.Text;
    
                        ds.Tables["notat"].Rows.Add(nrad);
                        ds.WriteXml("notat.xml", XmlWriteMode.WriteSchema);
                        ds.Clear();
                        ds.EnforceConstraints = false; 
                        ds.ReadXml("notat.xml", XmlReadMode.ReadSchema);
                     
                
                    
                    }
                   
                }
                else
                {
                    MessageBox.Show("Du har ikke skrevet noe i notatet, notatet kan ikke være tomt!");
               
                }
            }

    This is some of it

    • Edited by miniHessel Thursday, March 15, 2012 1:53 PM
    Thursday, March 15, 2012 1:50 PM
  • Yes because you actually added <name>1</name> twice in your Dataset.


    <tekst>{\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}} \viewkind4\uc1\pard\f0\fs17 qwe\par } </tekst> <name>1</name>

    and

        <tekst>qwe1</tekst>
        <name>1</name>

    When you defined <name> as Primary Key and Unique you cannot have multiple entries for that.


    Thursday, March 15, 2012 1:56 PM
  • Where? I can not see it..The code you pasted is from the XML file



    Thanks 


    When I delete all posts in the xml file and try to create one note, I get this in my XML. Seems like it saves it in the memory and write everything I had before x2?

      <notat>
        <tekst>{\rtf1\ansi\ansicpg1252\deff0\deflang1044{\fonttbl{\f0\fnil\fcharset0 Microsoft Sans Serif;}}
    \viewkind4\uc1\pard\f0\fs17 sad\par
    }
    </tekst>
        <name>asd</name>
      </notat>
      <notat>
        <tekst>sad</tekst>
        <name>asde</name>
      </notat>
      <notat>
        <tekst>sad</tekst>
        <name>qwewqeq</name>
      </notat>
      <notat>
        <tekst>sad</tekst>
        <name>qwewqeq</name>
      </notat>
    </notater>

    I think it is: 


     txtNotat.DataBindings.Add("Rtf", bloggBindingSource, "tekst", true);
    who does it! 
    • Edited by miniHessel Thursday, March 15, 2012 2:03 PM
    Thursday, March 15, 2012 1:59 PM
  • Sorry I didn't see your code.

    Firstly you defined 'ds' in different ways. Make sure you define it properly. According to your code you have one general 'ds' variable but is some cases like below you used another 'ds' . These two are not same.

     private void readFromKontaktTable()
            {
                DataSet ds = new DataSet();
                ds.ReadXml("notat.xml");
    
                listBox1.DataSource = ds.Tables["kontakt"];
                listBox1.DisplayMember = "navn";
                listBox1.ValueMember = "telefon";
    
    
    
            }

    if you want to use the 'ds' in the class you need to change

    DataSet ds = new DataSet();

    to

    ds = new DataSet();

    • Marked as answer by miniHessel Thursday, March 15, 2012 2:18 PM
    • Unmarked as answer by miniHessel Thursday, March 15, 2012 2:18 PM
    Thursday, March 15, 2012 2:12 PM
  • Done, but that didnt help :/ 


    the problem is that even if I delete stuff the dataset remembers all the posts, so when I add a new one, it writes everything back in x

    I think this is the problem:

       ds = new DataSet();
                    ds.ReadXml("notat.xml",XmlReadMode.ReadSchema);
                    bloggBindingSource = new BindingSource(ds, "notat");
                   txtNotat.DataBindings.Add("rtf", bloggBindingSource, "tekst", true);
                   txtName.DataBindings.Add("text", bloggBindingSource, "name", true);
    
                    bloggBindingSource.AllowNew = false;


    • Edited by miniHessel Thursday, March 15, 2012 2:35 PM
    Thursday, March 15, 2012 2:19 PM
  • Done, but that didnt help :/ 


    the problem is that even if I delete stuff the dataset remembers all the posts, so when I add a new one, it writes everything back in x

    I think this is the problem:

       ds = new DataSet();
                    ds.ReadXml("notat.xml",XmlReadMode.ReadSchema);
                    bloggBindingSource = new BindingSource(ds, "notat");
                   txtNotat.DataBindings.Add("rtf", bloggBindingSource, "tekst", true);
                   txtName.DataBindings.Add("text", bloggBindingSource, "name", true);
    
                    bloggBindingSource.AllowNew = false;




    I am adding a code here where the exact same thing happens..

    This code is much simpler and easier to understand:


    using System;
    using System.Data;
    using System.Windows.Forms;
    using MySql.Data;
    using MySql.Data.MySqlClient;
    
    namespace BloggWF
    {
        public partial class Form1 : Form
        {
            DataSet ds;
            MySqlDataAdapter da;
            BindingSource bloggBindingSource;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                ds = new DataSet("TEST");
                ds.ReadXml("blogg.xml",XmlReadMode.ReadSchema);
                bloggBindingSource = new BindingSource(ds,"blogg");
                txtOverskrift.DataBindings.Add("Text", bloggBindingSource, "overskrift",true);
                txtForfatter.DataBindings.Add("Text", bloggBindingSource, "forfatter", true);
                txtInnhold.DataBindings.Add("Text", bloggBindingSource, "innhold", true);
                txtDato.DataBindings.Add("Text", bloggBindingSource, "dato", true);
                bloggBindingSource.AllowNew = true;
    
            }
    
            private void btnOppdater_Click(object sender, EventArgs e)
            {
                DataRow nrad = ds.Tables["blogg"].NewRow();
                nrad["overskrift"] = txtOverskrift.Text;
                nrad["forfatter"] = txtForfatter.Text;
                nrad["innhold"] = txtInnhold.Text;
                nrad["dato"] = txtDato.Text;
                ds.Tables["blogg"].Rows.Add(nrad);
                ds.WriteXml("blogg.xml",XmlWriteMode.WriteSchema);
                ds.ReadXml("blogg.xml",XmlReadMode.ReadSchema);
            }
    
            private void btnNy_Click(object sender, EventArgs e)
            {
                // UPDATE
                txtOverskrift.Text = "";
                txtForfatter.Text = "";
                txtDato.Text = "";
                txtInnhold.Text = "";
            }
    
            private void btnFirst_Click(object sender, EventArgs e)
            {
                bloggBindingSource.MoveFirst();
            }
    
            private void btnPrevious_Click(object sender, EventArgs e)
            {
                bloggBindingSource.MovePrevious();
            }
    
            private void btnNext_Click(object sender, EventArgs e)
            {
                bloggBindingSource.MoveNext();
            }
    
            private void btnLast_Click(object sender, EventArgs e)
            {
                bloggBindingSource.MoveLast();
            }
        }
    }



    I add TWO posts ,but in the xml 3 are added..
    Thursday, March 15, 2012 4:06 PM
  • Hi miniHessel,

    I am moving your thread into the ADO.NET DataSet Forum for dedicated support.
    Have a nice day,

    Leo Liu [MSFT]
    MSDN Community Support | Feedback to us

    Monday, March 19, 2012 5:38 AM
  • Hi miniHessel,

    Welcome to MSDN Forum.

    Based on the issue, could you please upload the project to SkyDrive and post the link here? I will help you to test it.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, March 20, 2012 6:56 AM
    Moderator
  • I have fixed this..

    The problem was with my binding source.  Took huge time, but atleast it's working fine now. I clear the dataset and rebind bindings, thats my solution. 
    Tuesday, March 27, 2012 1:28 PM