Merging rows with Identity col into orig DataSet after updating DB

G

Guest

Hello,

I am trying to insert data into a database on the other end of a web
service. The primary key in my DataTable is an identity column. I first get
a copy of my dataset with only the changes in it by calling
DataSet.GetChanges. I then pass this little DataSet across the wire where my
insert stored procedure first inserts the records from the little DataSet
into the database and then the SELECT statements at the end of the stored
procedure updates my little DataSet so it gets the identity PK value from the
database. I then pass this little DataSet back to my client and want to
merge it with my original DataSet.

So lets say I insert a new row on the client, the DataSet will give it a PK
value of -1 (my AutoIncrementSeed and AutoIncrementStep values are -1), this
record is then sent to the server, the database is updated properly (via
DataAdapter.Update), the DataSet used for this is then updated with the
latest values from the data base (thanks to the SELECT statement at the end
of my stored procedures). At this point the PK value is now say 34 (from the
database). Now I ship this little DataSet back to the client and need to
merge its values with those in the bigger DataSet on my client. If I use
DataSet.Merge, the row with the -1 PK remains in the big DataSet and a new
row with PK of 34 is added to it.

What is the best way / standard way to merge in a situation like this?

Thanks, thanks, thanks

Nima
 
D

David Browne

Nima said:
Hello,

I am trying to insert data into a database on the other end of a web
service. The primary key in my DataTable is an identity column. I first
get
a copy of my dataset with only the changes in it by calling
DataSet.GetChanges. I then pass this little DataSet across the wire where
my
insert stored procedure first inserts the records from the little DataSet
into the database and then the SELECT statements at the end of the stored
procedure updates my little DataSet so it gets the identity PK value from
the
database. I then pass this little DataSet back to my client and want to
merge it with my original DataSet.

So lets say I insert a new row on the client, the DataSet will give it a
PK
value of -1 (my AutoIncrementSeed and AutoIncrementStep values are -1),
this
record is then sent to the server, the database is updated properly (via
DataAdapter.Update), the DataSet used for this is then updated with the
latest values from the data base (thanks to the SELECT statement at the
end
of my stored procedures). At this point the PK value is now say 34 (from
the
database). Now I ship this little DataSet back to the client and need to
merge its values with those in the bigger DataSet on my client. If I use
DataSet.Merge, the row with the -1 PK remains in the big DataSet and a new
row with PK of 34 is added to it.

What is the best way / standard way to merge in a situation like this?

On the client, you could remove all the rows with negative numbers for the
PK, and then merge.

David
 
J

JMM B

I had the same problem.
But, when doing the following, what is the second parameter, called
preserveChanges, for??
ds.merge(dsChanges, false)

I still have a problem with Default Values in the database.
when I don't fill the field in a Windows Forms DataGrid, the value goes
to null, and that's what the DataAdapter inserts in the DataBase. I need
to insert the default value for the column. I had a look in the property
"NullValue" of a table's column in a DataSet. It seems to do what I
want, but I didn't manage to make it work.

thanks,
 
G

Guest

Thanks David for the suggestion. When deleting a row that my binding manager
is pointing to, the binding manager ends up pointing to the previous row.
Then I have the problem of pointing the binding manager to the new row. I
can hack it, but this must be a common problem and I am pretty sure that it
has an elegant solution built into the framework that I am missing. Please
help.
 

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