Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7668247
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:18:53+00:00 2026-05-31T15:18:53+00:00

I found this bug while working with a DataTable. I added a primary key

  • 0

I found this bug while working with a DataTable.
I added a primary key column to a DataTable, than added one row to that table, removed that row, and added row with the same key to the table. This works. When I tried to call RejectChanges() on it, I got ConstraintException saying that value is already present.
Here is the example:

    var dataTable = new DataTable();
    var column = new DataColumn("ID", typeof(decimal));
    dataTable.Columns.Add(column);
    dataTable.PrimaryKey =  new [] {column };

    decimal id = 1;

    var oldRow = dataTable.NewRow();
    oldRow[column] = id;

    dataTable.Rows.Add(oldRow);
    dataTable.AcceptChanges();

    oldRow.Delete();

    var newRow = dataTable.NewRow();
    newRow[column] = id;

    dataTable.Rows.Add(newRow);
    dataTable.RejectChanges(); // This is where it crashes

I think since the row is deleted, exception should not be thrown (constraint is not violated because row is in deleted state). Is there something I can do about this? Any help is appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T15:18:55+00:00Added an answer on May 31, 2026 at 3:18 pm

    I assume that this has the same cause than following bug issue since the first that will be rejected is your delete action:

    DataTable.RejectChanges() should rollback rows in reverse order

    Two possible workarounds:

    Cycles through the DataRows rolling them back in reverse order. So the
    new records are removed before the previous ones are brought back to
    life.

    DataRowCollection rows = dataTable.Rows;
    for (int i = rows.Count - 1; i >= 0; i--)
    {
        rows[i].RejectChanges();
    }
    

    Disables constrains so the rollback can be done. Reenables constrains after that.

    1. You could use LINQ-to-DataSet to define your own “rollback-order”:

      var rollbackPlan = (from r in dataTable.AsEnumerable()
                     where r.RowState != DataRowState.Unchanged
                     let firstOrder  = r.RowState==DataRowState.Deleted? 1 : 0
                     let secondOrder = r.RowState==DataRowState.Added?   1 : 0
                     orderby firstOrder ascending, secondOrder ascending
                     select r).ToList();
      foreach (DataRow r in rollbackPlan)
      {
          r.RejectChanges(); // Does not crash anymore
      }
      
    2. Here’s the way you “disable” constraints on a DataTable temporarily:

      var constraintBackup = dataTable.Constraints.Cast<System.Data.Constraint>().ToList();
      dataTable.Constraints.Clear();
      dataTable.RejectChanges(); // Does not crash anymore
      foreach (System.Data.Constraint c in constraintBackup)
      {
          dataTable.Constraints.Add(c);
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on this yahoo pipe Regex and I found a bug I'm
Edit: This code is fine. I found a logic bug somewhere that doesn't exist
I just found something that sounds weird with Maven plugin management. While working on
Found this rather strange bug in IE8; element.style.top is limited to 1342177 pixels. Even
I found this open-source library that I want to use in my Java application.
Update: A sample project reproducing this bug can be found here at Microsoft Connect
I found this Perl script while migrating my SQLite database to mysql I was
Several times while debugging a VB.Net program I have found that continuation lines are
I've found a possible bug while i was using jquery and Ajax function on
I was working on my button template with this example: http://msdn.microsoft.com/en-us/library/ms753328.aspx I found the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.