3 Replies - 1086 Views - Last Post: 27 July 2011 - 12:14 PM Rate Topic: -----

#1 OdinVB20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-July 11

Deserializing data back to a dataset but data is not persisted to DB

Posted 27 July 2011 - 05:34 AM

Hello,

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 


Is This A Good Question/Topic? 0
  • +

Replies To: Deserializing data back to a dataset but data is not persisted to DB

#2 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,477
  • Joined: 07-April 08

Re: Deserializing data back to a dataset but data is not persisted to DB

Posted 27 July 2011 - 05:44 AM

Where is the code that you use to save the data back to the Database? I see where you put it into and get it out of the xml file, which you said was working fine, but I don't see your code used to save the information.
Was This Post Helpful? 0
  • +
  • -

#3 OdinVB20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-July 11

Re: Deserializing data back to a dataset but data is not persisted to DB

Posted 27 July 2011 - 05:51 AM

View Postrgfirefly24, on 27 July 2011 - 05:44 AM, said:

Where is the code that you use to save the data back to the Database? I see where you put it into and get it out of the xml file, which you said was working fine, but I don't see your code used to save the information.


Line 36 in the second chunk of code has the call to the table adapter update method - Me.Movie_tbTableAdapter.Update(Me.Movie_dbDataSet.movie_tb)
Was This Post Helpful? 0
  • +
  • -

#4 OdinVB20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 27-July 11

Re: Deserializing data back to a dataset but data is not persisted to DB

Posted 27 July 2011 - 12:14 PM

View PostOdinVB20, on 27 July 2011 - 05:51 AM, said:

View Postrgfirefly24, on 27 July 2011 - 05:44 AM, said:

Where is the code that you use to save the data back to the Database? I see where you put it into and get it out of the xml file, which you said was working fine, but I don't see your code used to save the information.


Line 36 in the second chunk of code has the call to the table adapter update method - Me.Movie_tbTableAdapter.Update(Me.Movie_dbDataSet.movie_tb)


I have included a screen shot where you can see that the data is getting back in to the dataset because I populate Datagrid2 with the data and even though I call the update method, it does not update the db. The 2 rows at the top are 2 rows that are already existing in the db. I added them through my app and the persisted to the db (which, by the way, is how I got the other 100 lines of data in to the db in the first place).

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1