none
Constraint failure when calling GetChanges() on DataTable. RRS feed

  • Question

  • Hi,

      I´m currently trying to call GetChanges() from a DataTable. In this case i´m getting a contraint failure, telling that there is an error in a parent relation. I have tried to disable constraints before calling the GetChanges method, but didn´t help. 

    I´m copying the piece of code...

        /// <summary>

        /// Check if the given BindingSource has at least one Row in edition mode.

        /// </summary>

        /// <param name="bindingSource"></param>

        /// <returns></returns>

        private bool GetUnderEdition(BindingSource bindingSource)

        {

          bool underEdition = false;

     

          if (bindingSource != null && bindingSource.DataSource != null)

          {

            DataView curDV = bindingSource.DataSource as DataView;

     

            //Cast has been successful

            if (curDV != null && curDV.Table != null)

            {

              //Disable constraints

              curDV.Table.DataSet.EnforceConstraints = false;

     

              //Call GetChanges() in the current table.

              DataTable curChangesTable = curDV.Table.GetChanges();

     

              if (curChangesTable != null && curChangesTable.Rows.Count > 0)

              {

                underEdition = true;

              }

     

              //Enable contraints

              curDV.Table.DataSet.EnforceConstraints = true;

            }

          }

     

          return underEdition;

        }

     The reason i´m using GetChanges() method is for efficiency...that is, i need to know if that specific table has at least 1 row that has been changed. If you know other efficient way to identify if a table has some modified/deleted/new row i will appreciate it. 

    Thanks in advance, 

    Paola

    Tuesday, August 10, 2010 12:41 PM

Answers

  • You don't say where you're getting the exception, but I bet it has nothing to do with your DataSet, but with your changed table. Try this instead:

    //Disable constraints
    curDV.Table.DataSet.EnforceConstraints = false;
    
    //Call GetChanges() in the current table.
    DataTable curChangesTable = curDV.Table.GetChanges();
    
    if (curChangesTable != null && curChangesTable.Rows.Count > 0)
    {
          underEdition = true;
    }
    
    //Enable contraints, but set changed table to null first
    curChangesTable = null;
    curDV.Table.DataSet.EnforceConstraints = true;
    
    

    I assume that you're doing this because only the DataSet has a .HasChanges() method, but you need to know if a specific DataTable has changes ...


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, August 10, 2010 8:04 PM
  • And wait ... here's another option:

    foreach (DataRow row in curDV.Table.Rows)
    {
    	if (row.RowState != DataRowState.Unchanged)
    	{
    		underEdition = true;
    		break;
    	}
    }
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, August 10, 2010 8:14 PM
  • Another thing you could try is simply handling the Table's RowChanged event, and set your underEdition property to true in the event handler.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, August 14, 2010 8:58 PM

All replies

  • You don't say where you're getting the exception, but I bet it has nothing to do with your DataSet, but with your changed table. Try this instead:

    //Disable constraints
    curDV.Table.DataSet.EnforceConstraints = false;
    
    //Call GetChanges() in the current table.
    DataTable curChangesTable = curDV.Table.GetChanges();
    
    if (curChangesTable != null && curChangesTable.Rows.Count > 0)
    {
          underEdition = true;
    }
    
    //Enable contraints, but set changed table to null first
    curChangesTable = null;
    curDV.Table.DataSet.EnforceConstraints = true;
    
    

    I assume that you're doing this because only the DataSet has a .HasChanges() method, but you need to know if a specific DataTable has changes ...


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, August 10, 2010 8:04 PM
  • And wait ... here's another option:

    foreach (DataRow row in curDV.Table.Rows)
    {
    	if (row.RowState != DataRowState.Unchanged)
    	{
    		underEdition = true;
    		break;
    	}
    }
    

    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Tuesday, August 10, 2010 8:14 PM
  • I have faced that sometime value of row has been changed but its status does not. at that time i check value of each column with previous one. I don't know why such things happens

    I need to keep my old valued dataset  or table somewhere and then compare each row column with new one..


    Thanks and Regards, Shailesh B. Davara
    Wednesday, August 11, 2010 5:47 AM
  • Sorry, I´m getting the exception when calling GetChanges() method (sentece:  DataTable curChangesTable = curDV.Table.GetChanges();). At this point the exception has to do with table relationships.  The message of the failure is the following "Cannot find the parent relation 'FK_Opportunity_ProductItems_ProductItem'." this means that i never get the point where i try to enable contraints again....and for some reason, it seems disabling the constraints is not helping :(.

    Yes, I have tried to call HasChanges on tables, but then i realized that this method is not available on DataTable class. 

    Regarding the other option that you suggested:


     foreach (DataRow row in curDV.Table.Rows)
     {
    	if (row.RowState != DataRowState.Unchanged)
    	{
    		underEdition = true;
    		break;
    	}
     }

    This is exactly what i implemented as first option in my app, but i got a significant performance degradation... 
    So, the main point here is that i need to know when i´m underEdition in an efficient way...pitty that there isn´t a HasChanges() for DataTables :(

    Wednesday, August 11, 2010 3:23 PM
  • Another thing you could try is simply handling the Table's RowChanged event, and set your underEdition property to true in the event handler.


    ~~Bonnie Berent [C# MVP]

    geek-goddess-bonnie.blogspot.com
    Saturday, August 14, 2010 8:58 PM