I have a situation where I serialized 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 in correct form all it is all there 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
VB2010 with SQL Server 2008 R2 (Express)
This is where the serialization occurs
Private Sub xmlBackupToolStripButton_Click(sender As System.Object, e As System.EventArgs) Handles xmlBackupToolStripButton.Click
Dim filePath As String = "C:\Users\Paul\Documents\Movie DB\Movie db Back Up\"
Dim fileName As String
Dim dateTime As String = CStr(DateAndTime.Now) ' this is some nice string manipulation - remove all "/" and all ":"
Dim button As DialogResult
Dim ser As XmlSerializer = New XmlSerializer(GetType(Movie_dbDataSet))
dateTime = dateTime.Replace("/", " ")
dateTime = dateTime.Replace(":", " ")
button = MessageBox.Show("Are you sure you want to make a backup file?", "Paul's Movie DB", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If button = DialogResult.Yes Then
'make the back up file with todays date and time as part of the file name
SaveDB() ' make sure you save the DB first to commit all changes
' recCount = Movie_tbBindingSource.Count
fileName = "movie DB Backup " + dateTime + " .xml"
filePath = filePath + fileName
If My.Computer.FileSystem.FileExists(filePath) Then
My.Computer.FileSystem.WriteAllText(filePath, String.Empty, False) 'if the file exists, make sure its empty before you back everything up to it
End If
'Serialize object to a text file.
Dim writer As TextWriter = New StreamWriter(filePath)
ser.Serialize(writer, Movie_dbDataSet)
writer.Close()
MessageBox.Show("Back Up Completed", "Paul's Movie DB", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
'return to the application
MsgBox("Back Aborted", MsgBoxStyle.Information, "Back Up Aborted")
Me.Show()
End If
End Sub
This is the deserialization code
Private Sub RestoreToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RestoreToolStripMenuItem.Click
Dim filePath As String = "C:\Users\Paul\Documents\Movie DB\Movie db Back Up\"
Dim fileName As String
Dim button As DialogResult
button = MessageBox.Show("Are you sure you want to RESTORE Paul's Movie DB?", "Paul's Movie DB", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)
If button = DialogResult.Yes Then
'restore the movie database from a file
'open the file dialogue and find the file you want to restore
RestoreOpenFileDialog.InitialDirectory = filePath
If RestoreOpenFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
fileName = RestoreOpenFileDialog.FileName
End If
Dim fStream As New StreamReader(fileName)
'RichTextBox1.Text = fStream.ReadToEnd
Dim mySerializer As XmlSerializer = New XmlSerializer(Movie_dbDataSet.GetType)
Movie_dbDataSet = CType(mySerializer.Deserialize(fStream), DataSet)
fStream.Close()
DataGridView2.DataSource = Movie_dbDataSet.movie_tb 'this worked great - the data is there
If Me.Validate Then
'MakeReadOnlyTrue()
Me.Movie_tbBindingSource.EndEdit()
Me.Movie_tbTableAdapter.Update(Me.Movie_dbDataSet.movie_tb)
'MessageBox.Show("Database Saved", "Save", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
System.Windows.Forms.MessageBox.Show(Me, "Validation errors occured.", "Save", _
System.Windows.Forms.MessageBoxButtons.OK, _
System.Windows.Forms.MessageBoxIcon.Warning)
End If
MessageBox.Show("Restore Completed", "Paul's Movie DB", MessageBoxButtons.OK, MessageBoxIcon.Information)
Else
'return to the application
MsgBox("Restore Aborted", MsgBoxStyle.Information, "Restore Aborted")
Me.Show()
End If
End Sub

New Topic/Question
Reply



MultiQuote






|