Rowstate changes from Added to Modified after it is inserted

G

Guest

Hello anyone,

I found the following strange behaviour when I tried to update my database
with an added row of the dataset. Stepping (debug mode) through the
RowUpdated event of the dataadpter I noticed the following event values:

AffectedRows=1
Statement = INSERT
rowstate = Modified

It is the last one that surprised me. How is it possible that the new
rowstate has become Modfied where it should remain Added until I call
AcceptChanges.

This behaviour causes problems since I update by dataset in parts:
deleted childs
deleted parents
inserted parents
modfied parents
inserted childs
updated childs

After the child (or parent) is inserted it is resubmitted since it has
changed to Modifed. (I use skipcurrentrow so no Acceptchanges are done
between the different updates)

Hope someone can shine their light on this.

WiZZiX
 
K

Kevin Yu [MSFT]

Hi WiZZiX,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you are checking the
SqlDataAdapter.RowUpdated event, you got wrong rowstate which doesn't match
the Statement. If there is any misunderstanding, please feel free to let me
know.

I have tried it but cannot repro on my computer, because the INSERT
statement response to the Added rowstate. Could you please check the
SqlDataAdapter setting to see if you have assigned the correct SQL
statement to SqlDataAdapter.UpdateCommand? The UpdateCommand has to be an
UPDATE statement.

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

Guest

Hello Kevin,

Thanks for trying to reproduce the problem.
I have checked the commands of the dataadapter and see no problems there.
Bluntly said I have encountered a situation in the RowUpdated event with the
following parameter values:

args.Statement = Insert
args.row.RowState = Modified

Perhaps it has to do with the fact that the storeprocedure that performs the
insert has output parameters aswell (Id, CreationDate etc..) If these values
are written to the row you could in a way state that the row is now Modified.

Since I set the Status to SkipCurrentRow the row is picked up again when I
run an update on all modified rows later on in the code.

I look forward to you insights on this,

Sandor (WiZZiX)
 
K

Kevin Yu [MSFT]

Hi Sandor,

What you have assumed might be true. Could you also check this in
SqlDataAdapter.RowUpdating event to see if it is the same as in the
SqlDataAdapter.RowUpdated event? Since RowUpdating event fires before the
row was updated, it might not be affected by the return value or output
parameter of the stored procedure. HTH.

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

Guest

Hello Kevin,

Your assumptions are correct. In the RowUpdating event the rowstate is still
Added. As soon as it reaches the RowUpdated event it changes to modified.

This behaviour shows in other places as well:
As I was trying to dataadapter.update a new Parent row and its NEW child
rows I noticed the change in rowstate for the childrows right after its
parent was inserted. I have all cascading options turned on and therefore
after inserting the parent row, it's newly received Id (from the DB) is
propogated to the child rows. The child rows should be inserted right after
that, using the new Id, but are updated using the UPDATE command. This is
because they have also changed to modfied as soon as they get their new Id.

How on earth am I going to add a new parent with new child records?

Hope you can elaborate on this some more,

Sandor
 
K

Kevin Yu [MSFT]

Hi Sandor,

I think there might be some misunderstanding here. If the child rows are
newly added, their RowState are Added. Before update, their RowState will
remain Added however we modify them. So when updating, we first update the
parent added records and then the child added records. I don't think they
will mess things up.

If it is still changing the child rows to Modified, I think there will be
other part of code that might be doing this.

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

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