473,327 Members | 1,967 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Updating an existing dataset using a diffgram

It seems like this should be easy, but it has proven to be quite
painful.

I have an existing dataset, which I used the GetChanges and WriteXML
functions upon to write out a DiffGram to a local XML file. I want to
use this same dataset (in another instance of the application) and the
DiffGram to display the changes in the DataGridView as they existed
after the user made his/her change.

In English:
A user will open the application, which displays data in a grid, make
their changes and hit save. This will save only the changes (in a
diffgram) to a local file. They will then come in the next day and
make a few more changes. I would like the application to grab the
original data and merge their previous changes into the original data
and display that on the grid.

I've used both ReadXML as well as Merge, but ReadXML only appends the
rows in the DiffGram to the end, and Merge doesn't seem to do anything
but toss the DiffGram square into the garbage.

Here is my code:

Dim dsList As New DataSet
Dim dsChanges As New DataSet

mySqlDataAdapter.AcceptChangesDuringFill = False
mySqlDataAdapter.Fill(dsList)

If File.Exists(strChanges) AndAlso
File.ReadAllText(strChanges).Length 0 Then

' ReadXml on the original dataset just adds it to the
end
'dsList.ReadXml(strChanges, XmlReadMode.DiffGram)
'.Tables(0).

dsChanges.ReadXml(strChanges, XmlReadMode.DiffGram)

dsList.Merge(dsChanges, True)

End If

grdResults.DataSource = dsList.Tables(0)

Please help!

Nov 20 '06 #1
1 4978
The solution is as painful as I had hoped...

Turns out that you need some form of unique ID (primary key) to match
the data, so I had to create a new column in the original dataset as an
identity field. You then need to set the primary key in both datasets
so that it knows how to match them. In addition, instead of merging
the changes back onto the original list, you must merge the list onto
the changes dataset. In order to do that, you need to grab the schema
of the original data set and set it to the changes schema. Confused?
Hopefully the code will be clearer than the description:

Dim mySqlDataAdapter As SqlDataAdapter = New
SqlDataAdapter(cmdList)

Dim dsList As New DataSet
Dim dsChanges As New DataSet

' Populate the list from the DB
mySqlDataAdapter.Fill(dsList)

' If the changes file exists (in diffgram format)
If File.Exists(strChanges) AndAlso
File.ReadAllText(strChanges).Length 0 Then

' Write out the schema from the list
File.WriteAllText("D:\temp\Schema.xml",
dsList.GetXmlSchema(), System.Text.Encoding.Unicode)

' Set the schema of the changes dataset
dsChanges.ReadXmlSchema("D:\temp\Schema.xml")

' Populate the changes dataset
dsChanges.ReadXml(strChanges, XmlReadMode.DiffGram)

' Set the primary key values for both datasets
dsList.Tables(0).PrimaryKey = New DataColumn()
{dsList.Tables(0).Columns("ID")}
dsChanges.Tables(0).PrimaryKey = New DataColumn()
{dsChanges.Tables(0).Columns("ID")}

' Merge the list onto the changes, not the other way
around
dsChanges.Merge(dsList, True)

' Set the datasource of the grid
grdResults.DataSource = dsChanges.Tables(0)
Else
' Set the datasource of the grid
grdResults.DataSource = dsList.Tables(0)
End If

Nov 21 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: IMS.Rushikesh | last post by:
Hi All, I want to serialize an object which contain the DataTable. As DataSet is MarshalByRef object and is serializable. I am using it directly. Even my data is also serialize and save it to...
1
by: Patrik | last post by:
Hi I want to return a dataset from a webservice and exclude the schema. How can I do this? /Patrik
0
by: Mav | last post by:
Hello, I'm trying to fill a dataset with the data of a diffgram xml-file. I expect a dataset with datasetname "ComputerSystem" filled with 1 table (tables.count = 1). The only thing i've got...
1
by: John Lee | last post by:
Hi, When I use DataSet.WriteXml(filename, XmlWriteMode.DiffGram), the xml file contains current values (at <InvDataSet> node) and original value (at <diffgr:before> node) - this means the...
0
by: JJ | last post by:
Hi, I have a DataSet that has no records in it from Database. I use this for just collecting records that I have been working on. I have a new worker dataset that goes out to database and gets a...
4
by: Brian Keating | last post by:
wonder if anyone can help me here, i've a framework 1.1 dataset which i serialize in framework 1.1 and deserialize in framework 2.0. This is fine, problem is that i want to modify some of the...
3
by: KillerJ | last post by:
I found a post about this exact issue: ...
6
by: Rich | last post by:
Dim da As New SqlDataAdapter("Select * from tbl1", conn) dim tblx As New DataTable da.Fill(tblx) '--works OK up to this point da.UpdateCommand = New SqlCommand da.UpdateCommand.Connection =...
13
by: processoriented | last post by:
I am trying to find the best approach to do something here... situation is this... One of the tables in my dataset is a "Work Order" table with information about what needs to be done and where. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.