DataAdapter not updating rows with RowState=Modified

M

Marco

Hi,
I have a strange problem. Most probably I am not seeing something huge,
so... please help.
Here's the problem:

I have a typed DataSet: MPDataSet
I must update a Row on the DB table Product, and the values of this Row are
passed to me through a Web Service, so I do not have the original DataSet.
Here's what I do:
MPDataSet MPDS= new MPDataSet();

//I create a datarow, fill in it the values passed from the WS, trick the DS
into modifying the RowsState to Modified and pass it to Update:

MarketPlaceDL.MPDataSet.ProductRow Row =MPDS.Product.NewProductRow();

Row.ID_Prod=ID_Prod;

///(...) I fill every field

MPDS.Product.Rows.Add(Row.ItemArray);

MPDS.Product.AcceptChanges();


MPDS.Product.LoadDataRow(Row.ItemArray,false); //to modify RowState

Now MPDS.Product[0].RowState==Modified, then I create a DataAdapter,
create its UpdateCommand and I call

DA.Update(MPDS.Product);

But nothing is updated, the query is never called BUT RowState==unchanged,
thus the DA called AcceptChanges(). I've checked the DA and seems OK.

The weird thing is that if I substitute



MarketPlaceDL.MPDataSet.ProductRow Row =MPDS.Product.NewProductRow();

Row.ID_Prod=ID_Prod;

///(...) I fill every field

MPDS.Product.Rows.Add(Row.ItemArray);



with a query like "Select * from product where id_prod="+ID_Prod the row is
populated (with the values as in the previous way) and, magic, the
subseguent call to Update() really updates!

This seems to show that the RowState=Modified is not enough to make the DA
call the UpdateCommand. IT seems like if the DA can tell if a Row really is
populated from a query. But since I used a Typed Dataset, I thought that it
had all the needed infos to map a row to its parent datatable...

Any clue? Thanks in advance, I am in deep muddy troubles..



Marco
 
W

William \(Bill\) Vaughn

Do not use AcceptChanges unless you know what it's used for.
You're telling ADO that the changes you made have already been (or should
not be) posted to the server.

--
____________________________________
William (Bill) Vaughn
Author, Mentor, Consultant
Microsoft MVP
www.betav.com
Please reply only to the newsgroup so that others can benefit.
This posting is provided "AS IS" with no warranties, and confers no rights.
__________________________________
 
M

Marco it

Do not use AcceptChanges unless you know what it's used for.
You're telling ADO that the changes you made have already been (or should
not be) posted to the server.

uhm, no. I use it before I do reload the row:

MPDS.Product.Rows.Add(Row.ItemArray);
//now RowState is added

MPDS.Product.AcceptChanges();
//now RowState is unchanged



MPDS.Product.LoadDataRow(Row.ItemArray,false); //to modify RowState
//now RowState is Modified

because I need to have Rowstate ==Modified to do the update. The problem is
that, even if the RowState is modified, the updatecommand is not called.

indeed as I stated on my post if I load for the first time the row with a
query to a database, all works, with or without acceptchanges.

did I misunderstand you?

tnx,
Marco
 
W

William Ryan eMVP

marco:

What is the specific intent of calling AcceptChanges. Also, from the looks
of it it doesn't look like the data has changees since the AcceptChanges and
the load row. In general too, you'll probably want to call AcceptChanges
on just the row itself in situations like this, unless you're sure you want
to reset all the rows...Unless there's something serious going on bettween
these three lines of code:
----
MPDS.Product.Rows.Add(Row.ItemArray);
//now RowState is added

MPDS.Product.AcceptChanges();
//now RowState is unchanged

MPDS.Product.LoadDataRow(Row.ItemArray,false); //to modify RowState
//now RowState is Modified
---'

It's kind of hard to follow the intent. If you just added the row as in the
first snippet and left out the rest, you'd have a row with the correct
rowstate.
--
W.G. Ryan MVP Windows - Embedded

Have an opinion on the effectiveness of Microsoft Embedded newsgroups?
Let Microsoft know!
https://www.windowsembeddedeval.com/community/newsgroups
 
M

Marco

Hi William.
I call AcceptChanges because if I don't, there is a runtime error:
System.Data.ConstraintException: Column 'ID_Product' is constrained to be
unique. Value '184' is already present.

anyway, the real problem is that doing what I do I have the row in the
correct rowstate, but it won't update! I mean, before I call the Update
method, the RowState is "Modified" that's correct, but the update is not
done! this is the thing that puzzles me.
This only if I create the row with CreateNewRow. if I create the row with a
query, the subsequent update is done correctely.


thanks,
I look forward to hear from you
 
A

Adrian Florea

Hi Marco,




After accepting changes (AcceptChanges), obviously, your DataSet has
no changes anymore (HasChanges is false). And with no changes, why do
you think Update would have to do something?



Adrian Florea
weblog: http://www.ugidotnet.org/7322.blog
 
M

Marco

Hi Adrian,
After accepting changes (AcceptChanges), obviously, your DataSet has
no changes anymore (HasChanges is false). And with no changes, why do
you think Update would have to do something?

simply because, as I said before, after AcceptChanges() I call:

MPDS.Product.LoadDataRow(Row.ItemArray,false);

which turns RowState=Modified and HasChanges() returns true.

so, there are changes, that's why I am puzzled because Update doesn't do
anything.

tnx.
m.

Adrian Florea
weblog: http://www.ugidotnet.org/7322.blog


MPDS.Product.AcceptChanges();


MPDS.Product.LoadDataRow(Row.ItemArray,false); //to modify RowState

Now MPDS.Product[0].RowState==Modified, then I create a DataAdapter,
create its UpdateCommand and I call

DA.Update(MPDS.Product);

But nothing is updated, the query is never called BUT RowState==unchanged,
thus the DA called AcceptChanges(). I've checked the DA and seems OK.
 

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