0

When I add a new row to my data set it is visible on that specific form in the datagridview, however when I switch to another form with the same data bound datagridview the new row is not there. When I close the program my new row is completely gone then. I want to to save the new row to the Access database it is reading from.

Public Class frmAddStudent




Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
    Me.Hide()
    frmUserControls.Show()
End Sub

Private Sub frmAddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblLecturer' table. You can move, or remove it, as needed.
    Me.TblLecturerTableAdapter.Fill(Me.StudentRecords1DataSet.tblLecturer)
    'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblStudents' table. You can move, or remove it, as needed.
    Me.TblStudentsTableAdapter.Fill(Me.StudentRecords1DataSet.tblStudents)

End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Dim MyNewRow As DataRow
    MyNewRow = StudentRecords1DataSet.tblStudents.NewRow



    Try

        With MyNewRow
            .Item(1) = txtID.Text
            .Item(2) = txtFirstName.Text
            .Item(3) = txtSurname.Text


        End With



        Me.Validate()
        Me.TblStudentsBindingSource.EndEdit() 'Change this to your binding source ' eg table'
        Me.TableAdapterManager.UpdateAll(Me.StudentRecords1DataSet) ' chnage this to your database name'
        MessageBox.Show("The Data Has Been Saved", "Information", MessageBoxButtons.OK)
    Catch ex As Exception
        'if there is a problem saving the data, it will show a messagebox with the problem as to why it could'nt save the data'
        MessageBox.Show(ex.Message)
    End Try

    StudentRecords1DataSet.tblStudents.Rows.Add(MyNewRow)
    StudentRecords1DataSet.tblStudents.AcceptChanges()

End Sub

Private Sub txtFirstName_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs)

End Sub

Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigator.RefreshItems

End Sub

End Class

Any suggestions please?

Donncha
  • 43
  • 1
  • 7

1 Answers1

1

UPDATe X2 based off your question update. try this,

Public Class frmAddStudent


Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
    Me.Hide()
    frmUserControls.Show()
End Sub

Private Sub frmAddStudent_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblLecturer' table. You can move, or remove it, as needed.
    Me.TblLecturerTableAdapter.Fill(Me.StudentRecords1DataSet.tblLecturer)
    'TODO: This line of code loads data into the 'StudentRecords1DataSet.tblStudents' table. You can move, or remove it, as needed.
    Me.TblStudentsTableAdapter.Fill(Me.StudentRecords1DataSet.tblStudents)

End Sub

Private Async Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click

    Try
        Me.Validate()
        Me.TblStudentsBindingSource.EndEdit() 'Change this to your binding source ' eg table'
        Me.TableAdapterManager.UpdateAll(Me.StudentRecords1DataSet) ' chnage this to your database name'
        MessageBox.Show("The Data Has Been Saved", "Information", MessageBoxButtons.OK)
        Await Task.Delay(100)
        TblStudentsBindingNavigator_RefreshItems.studentsBindingSource.AddNew()
    Catch ex As Exception
        'if there is a problem saving the data, it will show a messagebox with the problem as to why it could'nt save the data'
        MessageBox.Show(ex.Message)
    End Try

End Sub

Private Sub txtFirstName_MaskInputRejected(sender As Object, e As MaskInputRejectedEventArgs)

End Sub

Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As EventArgs) Handles TblStudentsBindingNavigator.RefreshItems

End Sub

End Class

user1234433222
  • 976
  • 2
  • 10
  • 21
  • I have added in that Try Catch, however I am still having the same issue of the data not saving to my db. Your message box saying the data has saved is appearing but rows are not actually being saved – Donncha Mar 25 '16 at 11:24
  • can you add all of your code to your question please? there must be a problem somewhere else, yeah it will save completed if the database hasn't been setup correctly. – user1234433222 Mar 25 '16 at 11:26
  • did you drag the grid across from "Data Sources"? – user1234433222 Mar 25 '16 at 11:36
  • also the way you have it making a new row is incorrect, you should try something like `Customers_InformationBindingSource.AddNew()` and it will automatically add a new row for the access database. – user1234433222 Mar 25 '16 at 11:39
  • Yes that grid is dragged across and so were the text boxes – Donncha Mar 25 '16 at 11:57
  • if it doesnt work, feel free to email me your code and access datafile and i will take a look for you :) – user1234433222 Mar 25 '16 at 12:01
  • Pasted in your new answer, getting an eror with `TblStudentsBindingNavigator_RefreshItems.studentsBindingSource.AddNew()` . Error is `Argument not specified for parameter 'sender' of 'Private Sub TblStudentsBindingNavigator_RefreshItems(sender As Object, e As System.EventArgs` – Donncha Mar 25 '16 at 12:04
  • send me your project with the access data file and ill have a real look for you. = andreweberle@outlook.com – user1234433222 Mar 25 '16 at 12:21
  • Sent! Let me know how you get on – Donncha Mar 25 '16 at 12:28
  • OK, i have sent you an email with an example i have made up, i think the problem you have is with your database from Access, let me know how you go :) Good luck! – user1234433222 Mar 25 '16 at 14:51
  • Thanks a million for all your help @Werdna, greatly appreciated and I understand the VB to DataBase a lot better now!! `(Y)` – Donncha Mar 27 '16 at 02:51
  • @Donncha no problem at all, happy to help :) – user1234433222 Mar 27 '16 at 07:13