Question Deserializing Data Back To A Dataset But Data Is Not Persisted To DB

Odin

Member
Joined
Jul 27, 2011
Messages
7
Programming Experience
1-3
Hello All,
I have a situation where I serialize data from a dataset to an xml file as a method of backup. I am trying to get the data deserialized back to the dataset.
But for some reason, I cannot get the data to persist back to the DB. I don't receive any error messages either. The first chunk of code is the serialization code and the second chunk is the deserialization code. I can show that the deserialization back to the dataset is complete because this code in the 2nd block - DataGridView2.DataSource = Movie_dbDataSet.movie_tb - displays the data and it looks correct in the datagridview2. I call the update method but it does not get persisted back to the db. I must be looking at the problem but can't see it. I have included a screen shot that shows the dataset has the restored data in it but it does not persist to the db.

VB2010 with SQL Server 2008 R2 (Express)

Serialization Code pic.png
Deserialize Code pic.png

Screen shot after running.jpg
 
Last edited:
What is the return value for the Update call? It returns "The number of rows successfully updated from the DataTable".

dataset.WriteXml/ReadXml methods is simpler for xml serialization.

How do you manage to get the posted code mangled like that? It's just a simple copy-paste operation from VS.
 
What is the return value for the Update call? It returns "The number of rows successfully updated from the DataTable".

dataset.WriteXml/ReadXml methods is simpler for xml serialization.

How do you manage to get the posted code mangled like that? It's just a simple copy-paste operation from VS.

I don't know why the code didn't stay formatted. It was formatted correctly when I pasted it (each of the 4 times I tried) but as soon as I pressed Save, the formatting disappeared. i am working in the WYSIWYG mode and put the code between Xcode tags. I was working in Chrome but for this post, I switched to IE9 to see if there were any differences.

The return value is 0 from the restore sub but when I added 1 line through the interface, the return value was 1. To note that the restore code is on a different form than the main form - I moved it there thinking that I might be able to figure out the problem easier without looking at all of the other code. It didn't work when I had the restore code in the main form either. The funny thing is that I just copied the update code from my Save sub.
 
Last edited:
SaveDB() ' make sure you save the DB first to commit all changes
If you have saved all changes first most likely all rows in dataset gets RowState Unchanged, so later when attempting to Update there is no changed rows and nothing is sent to db.
 
If you have saved all changes first most likely all rows in dataset gets RowState Unchanged, so later when attempting to Update there is no changed rows and nothing is sent to db.

Hi John and thanks for looking at this for me. The Save DB line is in the serialization code. I save the db to make sure that all changes are in the DB and then I serialize it. In the original post, I now have added jpegs of the code that are much more readable.
 
If you have saved all changes first most likely all rows in dataset gets RowState Unchanged, so later when attempting to Update there is no changed rows and nothing is sent to db.

John,

I have really simplified things to help diagnose my issue. I implemented your suggestion of using dataset.readxml and it worked nicely in getting the data back in to the dataset. I can scroll through all 103 movies (3 test ones that I have had through this whole issue plus the 100 I restored). I can make changes to any of the original 3 and they are persisted to the db. BUT if I make a change to any of the 100, now I get a DBConcurrencyException

Troubleshooting Exceptions: System.Data.DBConcurrencyException]Visual Studio 2010

The exception that is thrown by the DataAdapter during the update operation if the number of rows affected equals zero.

Here is my new sub to restore - 1 line of code and then goes to my save routine:


Private
Sub MainRestoreToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles MainRestoreToolStripButton.Click

Movie_dbDataSet.ReadXml("C:\Users\Paul\Documents\Movie DB\Movie db Back Up\movie DB Backup 24 07 2011 4 28 07 PM .xml")
SaveDB()

End
Sub
 
Solved!!!!

Problem Solved!!

Using dataset.writexml solved the problem because the method I was using exported the schema and the data, whereas dataset.writexml exported just the data. I still don't understand why I didn't get an error of some kind when I tried to import the data back in? Thanks for your help John - your suggestion pushed me to the answer.

Cheers,
Paul
 
WriteXml can specify XmlWriteMode.WriteSchema, but that is not necessary here since the typed dataset already contains the schema.
The XmlSerializer you used outputted schema and a diffgram, so previously accepted rows read back would have RowState Unchanged as I said.
WriteXml can also specify XmlWriteMode.DiffGram which includes rowstate information, for the other two modes all rows read back will get RowState Added.
I still don't understand why I didn't get an error of some kind when I tried to import the data back in?
The data output of XmlSerializer and WriteXml is no different, so ReadXml can read both.
 
Hi John,
I am working on a project that uses LINQ to SQL and need to do the same backing up to an xml file concept as I did with the dataset method. Should I use the dataset.xmlwrite process again or is there a more elegant way using LINQ to SQL. So far, I have not been able to find one.
Paul
 
Don't know, I would have to research it. I know it is easy to generate xml with Linq, but can't see that DataContext has a simple WriteXml method or similar. Perhaps one possibility is to use XmlSerializer to serialize the data objects.
 

Latest posts

Back
Top