DataTable.RemoveRow

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi
I have a form with a datagrid whose source datatable is filled WITHOUT BEING FIRST CLEARED each time the use
selects a different value in a Parent Listbox control. The datagrid's source datatable has Primary Key constraints. I would like to
be able to capture the Data.ConstraintException that is thrown is a user tries to insert a duplicate row in the datatable, and auto
matically remove the duplicate row. I am wondering what the best strategy would be to do this? Should I use a Try..Catch..EndTry
in the listbox's selected value changed handler, which is where the source datatable is filled? I think this is the right track, but I a
a little lost on where to go from here. Any help would be appreciated
JT
 
Hi JT,

Thank you for posting in the community!

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that you need to fill a DataTable which
contains data already, and needs to remove the duplicated rows. If there is
any misunderstanding, please feel free to let me know.

As far as I know, when you fill a DataTable which has a primary key
constraint, the original row will be overwritten by the new row
automatically if there are rows containing the same primary key. There
won't be any Data.ConstraintException thrown during filling. This is the
behavior of DataAdapter.Fill() method by design.

I'd like to know which row you would like to remove. The original one or
the new one? If you are going to remove the original one, no efforts are
needed. Everything will be done by the DataAdapter automatically.

For more information about this behavior, please check the following link
for reference:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatacommondbdataadapterclassfilltopic2.asp

Does this answer your question? If anything is unclear, please feel free to
reply to the post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Hi JT,

I have retried to reproduce the contraint exception according to your
description, but failed. As far as I know, the unique constraint only works
when you are adding rows to the table. It doesn't affect the
DataAdapter.Fill method.

I think the best way to merge the data, is to import rows one by one in a
loop. We can fill the new rows into another DataSet and use
DataTable.ImportRow method to get the rows from the new DataSet. When the
constraint is violate, an ConstraintException is thrown. We can handle the
exception and do nothing in the exception handler. The row will not be
added to the table. After the loop, all valid rows will be added to the
table. Here is an example:

foreach(DataRow dr in dataset2.Table1.Rows)
{
try
{
this.dataSet1.Table1.ImportRow(dr);
}
catch(ConstraintException)
{
}
}

Hope this helps. If this doesn't work, please feel free to let me know. I'm
standing by to be of assistance.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Thanks Kevin
I am surprised you are unable to recreate my exception. Perhaps it has to do with my dataset architecture. The dataset contains four tables, which have relationships to one another. Table Claims is parent to Claims Adjustments and Service Line, and Service Line is parent to Service Line Adjustments. When the user clicks on a Claim Number (the Primary Key for Claims) in the listbox, the four datatables fill using DataAdapter.Fill. If the user clicks on the same Claim Number a second time, the DataAdapter.Fill adds the identical row to the Claims table, and fires a ConstraintException. Of course, this would not happen if I cleared my datatable before calling DataAdapter.Fill, but I do not want to clear the datatable, I just want to prevent duplicate rows from appearing. As I mentioned, I am now removing the Claim Number from the listbox once the user clicks on it, thus circumventing the error before it can happen. I remain curious as to how to handle the actual error though
Thanks
JT
 
Hi JT,

If the exception was thrown during filling the original DataSet, I don't
think we can do anything to it. Because the filling process has been
terminated whenther we can handle the exception.

So my suggestion is to fill the new data to anther DataSet and import the
records in a loop with DataTable.ImportRow method from the original
DataSet. When importing, if an constraint exception is thrown, we can
handle it in the catch block. In the catch block, we can do anything, such
as count how many duplicated rows. Or we can just do nothing to bypass that
row. Thus, we have achieved our goal to keep the original row while the new
row has been removed. You can try my code in my last post.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top