Help - Dataset.Getchanges(DataRowState.Deleted)

G

Guest

Hi All,

I can get values from a dataset that have been changed by using the
following ;

Dim tempDataSet As DataSet = _
dataSet.GetChanges(DataRowState.Modified)

but I want deleted rows.

So I try

Dim tempDataSet As DataSet = _
dataSet.GetChanges(DataRowState.Deleted)

But I dont get any of the results,from reading I can see that the datarows
are only flagged for deletion and not removed from the dataset. I would have
thought using the GetChanges with DataRowState.Deleted would have returned
the rows.

What is the best way to get the deleted rows.?


Thanks

Neil
 
W

W.G. Ryan [MVP]

Neil:

I'm not sure I understand the problem. You want the Deleted rows but they
aren't coming back? Has AcceptChanges been called or has the DataSet been
updated? How are you deleting the Rows. With Delete or Remove? If Remove,
then that's the problem. That takes them out of the collection as opposed to
marking them for deletion
 
G

Guest

Hi , Thanks for reply.

I am using the datagrid, just selecting the row and deleting the item in
question. I can look through the original dataset but can not get the row
details of the deleted ones. I can iterate through the row collection and
using the rowsate show how many have been deleted. Im using VS2005 just
incase this makes a difference.

Thanks

Neil
 
W

W.G. Ryan [MVP]

Are any updates getting called, for instance if this is an asp.net, is there
a delete command associated with it? We're missing something
straightforward here, I've been down this road a few times myself;-)

So basically you can iterate and see x Deleted rows, but if youuse
GetChanges and specify just the deleted ones, nothing?
 
G

Guest

Its Windows forms , Im going to see if I can overide the delete on the
datagrid and handle it myself, maybe the datagrid is my problem.

I wish Getchanges would just get everything that would be so much easier, :).

Thanks

Neil
 
W

W.G. Ryan [MVP]

Neil Steventon said:
Its Windows forms , Im going to see if I can overide the delete on the
datagrid and handle it myself, maybe the datagrid is my problem.

I wish Getchanges would just get everything that would be so much easier,
:).
Neil:

It should be getting them.I'm just wondering if there's an Update getting
called somewhere or acceptchanges. One thing you might try just to try to
nail down the problem. Create a DataView and set the RowStateFilter to
deleted. See if you get anything with that. I was thinking if you wereusing
typed datasets that a new table might be getting created, but if that's the
code you're using below, that's not the case. Just to be sure though, check
the DataSet.Tables.Count property to make sure nothing funky is going on
like that.
 
G

Guest

The error im getting is

DeletedRowInaccessibleException
Represents the exception that is thrown when an action is tried on a DataRow
that has been deleted.

The code I have used to test the row state is as follows

Dim dtRow as DataRow
For each dtRow in _myDataSet.Tables("Table1").Rows
if dtRow.RowState = DataRowState.Deleted then
'Here is where I thought I could access the row columns etc, but
the
'exception is thrown.
End if
'Next

Using a dataview yep I can get the values by using the rowstate filter. Ill
see if there is an explanation on how the datagrid deletes the row.

Thanks for your posts.
 
G

Guest

Managed to work out that I need to use the original version.

if dtRow.RowState = DataRowState.Deleted then
Msgbox(dtRow(0, DataRowVersion.Original).ToString)
End if

God, I think I will have to write a new get changes that returns deleted
rows also.

Thanks for all your replies.
 
J

Jim Rand

Here is a little wrinkle to add to the mix.

Assume:
- three related tables: customer, invoice, invoice detail in a
hierarchical grid
- concurrency is managed via timestamps [WHERE ... AND CAST(TS AS INT) =
timestampindatarowcastasinteger]
- order of processing is deletes first up the chain, adds next down the
chain, modified next down the chain
- three data adapters - one for each table
- UpdateOperation performs dataAdapter.Update(dsSubset, tablename); //
dsSubset contains either dsDeleted, dsAdded, dsModified

Outer logic is:
DataSet dsDeleted = ds.GetChanges(DataRowState.Deleted);
DataSet dsAdded = ds.GetChanges(DataRowState.Added);
DataSet dsModified = ds.GetChanges(DataRowState.Modified);

UpdateOperation(dsDeleted, adapterList, DataRowState.Deleted);
UpdateOperation(dsAdded, adapterList, DataRowState.Added);
UpdateOperation(dsModified, adapterList, DataRowStateModified);

if (dsDeleted != null) ds.Merge(dsDeleted, false);
if (dsAdded != null) ds.Merge(dsAdded, false);
if(dsModified != null) dsMerge(dsModified, false);

Now assume:
- Change a field value for the customer (row passed to dsModified)
- Add an invoice (row passed to dsAdded)

Question: Will the overall update succeed or fail?

Answer: It will fail with a concurrency exception for the
UpdateOperation(dsModified...)

Why: dsAdded includes the invoice row as expected, but it also includes the
parent customer row for the added invoice. As a result, the customer row
gets processed twice - first with the adds and then with the modifies.

Workaround:

// dataAdapter.Update(dsAdded, tableName);
foreach (DataRow row in dsAdded.Tables[tableName].Rows)
{
if (row.RowState == DataRowState.Added) dataAdapter.Update(new
dataRow[] { row });
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top