Steve

Steve

  • NA
  • 2
  • 0

When I reopen a dataset, I can't add additional records

Jul 11 2009 10:38 AM

I'm practicing a database and I have a 3-column table. I'm using binary formatting to save/open the file. I can't see what is wrong with my code โ€“

I can display records ok when I press the save button

I can also save the records ok when the save/close button is pressed.

When I reopen the programme and press the reload button to reload the dataset, additional records will not be displayed nor saved though original records will be showing.

When I reopen the programme and DO NOT press the reload button, I can enter new records and they will be saved because this is basically overwrites the original dataset.

Ideally, I want to be able to open the dataset and then be able to add new records to the dataset/datatable.

Any help and advice on how to correct this?

Thanks,

steve

 

 

Imports System.IO

Imports System.Runtime.Serialization

Imports System.Runtime.Serialization.Formatters.Binary

 

Public Class Frm_DataEntry

    Dim ThePoint As New Point(540, 150)

    Public TheRecord As DataRow

    Dim ii As Integer

 

    Private Sub Frm_DataEntry_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Me.Location = ThePoint

    End Sub

 

    Private Sub Tbx01_FirstName_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Tbx01_FirstName.Enter

        Tbx01_FirstName.Clear()

        Tbx02_LastName.Clear()

        Tbx03_Phone.Clear()

    End Sub

 

    Private Sub Btn01_Save_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn01_Save.Click

        TheRecord = Frm_Mainform.dt_Table.NewRow

 

       

        TheRecord(Frm_Mainform.dt_Col02_FirstName) = Tbx01_FirstName.Text

        TheRecord(Frm_Mainform.dt_Col03_LastName) = Tbx02_LastName.Text

 

        Frm_Mainform.dt_Table.Rows.Add(TheRecord)

        Frm01_WithDGV.DGV01.DataSource = Frm_Mainform.ds_Dataset

        Frm01_WithDGV.DGV01.DataMember = Frm_Mainform.dt_Table.ToString

 

        Frm01_WithDGV.DGV01_BS_Col01_PKey.DataPropertyName = Frm_Mainform.dt_Col01_PKey.ToString

        Frm01_WithDGV.DGV01_BS_Col02_FirstName.DataPropertyName = Frm_Mainform.dt_Col02_FirstName.ToString

        Frm01_WithDGV.DGV01_BS_Col03_LastName.DataPropertyName = Frm_Mainform.dt_Col03_LastName.ToString

 

        Dim ii As Integer

  ii = Frm_Mainform.dt_Table.Rows.Count

        MessageBox.Show(ii)

 

    End Sub

 

    Private Sub Btn02_SaveAndClose_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn02_SaveAndClose.Click

        Dim Filename As String = "PermitToWork.pmt"

        Dim fStream As FileStream

 

        Frm01_WithDGV.DGV01.DataSource = Frm_Mainform.ds_Dataset

        Frm01_WithDGV.DGV01.DataMember = Frm_Mainform.dt_Table.ToString

 

        Frm01_WithDGV.DGV01_BS_Col01_PKey.Width = 90

        Frm01_WithDGV.DGV01_BS_Col02_FirstName.Width = 125

        Frm01_WithDGV.DGV01_BS_Col03_LastName.Width = 125

        Frm01_WithDGV.DGV01_BS_Col04_Date.Width = 125

        Frm01_WithDGV.DGV01_BS_Col01_PKey.DataPropertyName = Frm_Mainform.dt_Col01_PKey.ToString

        Frm01_WithDGV.DGV01_BS_Col02_FirstName.DataPropertyName = Frm_Mainform.dt_Col02_FirstName.ToString

        Frm01_WithDGV.DGV01_BS_Col03_LastName.DataPropertyName = Frm_Mainform.dt_Col03_LastName.ToString

 

        If File.Exists(Filename) Then

            Try

                fStream = New FileStream(Filename, FileMode.Create)

                Dim binFormat As New BinaryFormatter

                binFormat.Serialize(fStream, Frm_Mainform.ds_Dataset)

            Catch anex As ArgumentNullException

                MsgBox("The inventory could not be accessed")

            Catch ex As SerializationException

                MsgBox("The application failed to retrieve the inventory")

            Finally

                fStream.Close()

            End Try

        Else

            Return

        End If

 

        Frm_Mainform.Close()

    End Sub

 

    Private Sub Btn03_Reload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Btn03_Reload.Click

        Dim Filename As String = "PermitToWork.pmt"

        Dim fStream As FileStream

 

        If File.Exists(Filename) Then

            Try

                fStream = New FileStream(Filename, FileMode.OpenOrCreate, FileAccess.ReadWrite)

                Dim binFormat As New BinaryFormatter

                Frm_Mainform.ds_Dataset = binFormat.Deserialize(fStream)

 

                Frm01_WithDGV.DGV01.DataSource = Frm_Mainform.ds_Dataset

                Frm01_WithDGV.DGV01.DataMember = Frm_Mainform.dt_Table.ToString

 

                Frm01_WithDGV.DGV01_BS_Col01_PKey.Width = 90

                Frm01_WithDGV.DGV01_BS_Col02_FirstName.Width = 125

                Frm01_WithDGV.DGV01_BS_Col03_LastName.Width = 125

                Frm01_WithDGV.DGV01_BS_Col04_Date.Width = 125

                Frm01_WithDGV.DGV01_BS_Col01_PKey.DataPropertyName = Frm_Mainform.dt_Col01_PKey.ToString

                Frm01_WithDGV.DGV01_BS_Col02_FirstName.DataPropertyName = Frm_Mainform.dt_Col02_FirstName.ToString

                Frm01_WithDGV.DGV01_BS_Col03_LastName.DataPropertyName = Frm_Mainform.dt_Col03_LastName.ToString

 

            Catch anex As ArgumentNullException

                MsgBox("The inventory could not be accessed")

            Catch ex As SerializationException

                MsgBox("The application failed to retrieve the inventory")

            Finally

                fStream.Close()

            End Try

        Else

            Return

        End If

 

        Frm01_WithDGV.DGV01_BS_Col01_PKey.Width = 90

        Frm01_WithDGV.DGV01_BS_Col02_FirstName.Width = 125

        Frm01_WithDGV.DGV01_BS_Col03_LastName.Width = 125

        Frm01_WithDGV.DGV01_BS_Col04_Date.Width = 125

 

        Dim ii As Integer

        ii = Frm01_WithDGV.DGV01.Rows.Count

        MessageBox.Show(ii)

 

    End Sub

 

End Class