0
votes

I'm new to VB. I want to refresh my DataGridView on my frmSearchUpdateBook when I add data from frmAddBook. When the data is added, the DataGridView on my frmSearchUpdateBook is not refreshed and the added data is not there, it needs to re-run the application to view the new added data. Can someone helpe me?

Here's my codes:

Imports System.Data.OleDb

Public Class frmAddBooks

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    Dim cn As New OleDbConnection
    Dim cm As New OleDbCommand

    Try
        With cn
            .ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Assessment of the CKC Library System\Database.accdb"
            .Open()
        End With

        With cm
            .Connection = cn
            .CommandType = CommandType.Text
            .CommandText = "INSERT INTO [Books] ([ISBN],[BookTitle],[Author],[Category],[Foreword],[YearPublished], [Quantity]) VALUES ('" & txtISBN.Text & "', '" & txtBookTitle.Text & "', '" & txtAuthor.Text & "', '" & cboCategory.Text & "', '" & txtForeword.Text & "', '" & cboYearPublished.Text & "', '" & cboQuantity.Text & "')"

            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@ISBN", System.Data.OleDb.OleDbType.VarChar, 30, Me.txtISBN.Text))
            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@BookTitle", System.Data.OleDb.OleDbType.VarChar, 255, Me.txtBookTitle.Text))
            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@Author", System.Data.OleDb.OleDbType.VarChar, 255, Me.txtAuthor.Text))
            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@Category", System.Data.OleDb.OleDbType.VarChar, 255, Me.cboCategory.Text))
            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@Foreword", System.Data.OleDb.OleDbType.VarChar, 255, Me.txtForeword.Text))
            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@YearPublished", System.Data.OleDb.OleDbType.VarChar, 255, Me.cboYearPublished.Text))
            .Parameters.Add(New System.Data.OleDb.OleDbParameter("@Quantity", System.Data.OleDb.OleDbType.VarChar, 255, Me.cboQuantity.Text))

            'RUNS THE COMMAND
            cm.Parameters("@ISBN").Value = Me.txtISBN.Text
            cm.Parameters("@BookTitle").Value = Me.txtBookTitle.Text
            cm.Parameters("@Author").Value = Me.txtAuthor.Text
            cm.Parameters("@Category").Value = Me.cboCategory.Text
            cm.Parameters("@Foreword").Value = Me.txtForeword.Text
            cm.Parameters("@YearPublished").Value = Me.cboYearPublished.Text
            cm.Parameters("@Quantity").Value = Me.cboQuantity.Text




            cm.ExecuteNonQuery()
            MsgBox("Book added.", MsgBoxStyle.Information, "Saved Successfully!")
            Me.txtISBN.Text = ""
            Me.txtBookTitle.Text = ""
            Me.txtAuthor.Text = ""
            Me.txtForeword.Text = ""
            cboCategory.SelectedIndex = -1
            cboQuantity.SelectedIndex = -1
            cboYearPublished.SelectedIndex = -1

            Exit Sub
        End With

    Catch ex As Exception
        MsgBox("Please fill all the details needed and be sure that the ISBN doesn't have any letters. ", MsgBoxStyle.Exclamation, "Error")


    End Try
End Sub

Private Sub btnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
    Hide()
End Sub

End Class


Public Class frmSearchUpdateBooks

Private Sub BooksBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
    Me.Validate()
    Me.BooksBindingSource.EndEdit()
    Me.TableAdapterManager.UpdateAll(Me.DatabaseDataSet)

End Sub

Private Sub frmSearchUpdateBooks_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    'TODO: This line of code loads data into the 'DatabaseDataSet.Books' table. You can move, or remove it, as needed.
    Me.BooksTableAdapter.Fill(Me.DatabaseDataSet.Books)

End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Try
        BooksBindingSource.EndEdit()
        BooksTableAdapter.Update(DatabaseDataSet)
        MsgBox("Successfully saved.", MsgBoxStyle.OkOnly, "Success!")
        DataGridView1.Refresh()

    Catch ex As Exception
        MsgBox("Please fill all the information needed. If all informations are filled up, please re-check the ISBN as some book might have the same ISBN as you are entering.", MsgBoxStyle.OkOnly, "Error!")
    End Try
End Sub

Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
    BooksBindingSource.RemoveCurrent()
    BooksBindingSource.EndEdit()
    MsgBox("Item successfully deleted!", MsgBoxStyle.OkOnly, "Success!")
End Sub

Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
    Try
        Me.Validate()
        Me.BooksBindingSource.EndEdit()
        Me.BooksTableAdapter.Update(DatabaseDataSet)
        MsgBox("Updated Successfully!", MsgBoxStyle.OkOnly, "Update Successful!")
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
        Me.BooksTableAdapter.Fill(DatabaseDataSet.Books)
    End Try
End Sub

End Class


I can update the DataGridView if I'm using 1 form only but my professor wants me to use 2 forms. One for adding books, one for datagridviewing. Thank you!

1
I can't find a VB example, but you probably want to raise an event in the form that inserts and subscribe to it in the form with the grid so you can issue a new query for data. - Crowcoder
I've read somewhere in a post that it needs to re-query. Which is I, obviously, don't know how since I'm just a month old in VB. - Rey Anthony Abarquez Rosalado
The event handler is where you would requery. That is not the only way though. You could maintain a reference to the grid's bindingsource on the other form and refresh it from there. There are tons of questions on SO about sharing data between forms. - Crowcoder
I cant seem to find the right answer to my problem. I just wanna refresh the data grid on my 2nd form when I added a book on my first form. There are some, but it's on C#. Thank you tho. - Rey Anthony Abarquez Rosalado
If the 2 forms use the same DataSource, perhaps with a different filter, there is no need to requery, no need to refresh, no need to do much at all. Please read How to Ask and take the tour - Ňɏssa Pøngjǣrdenlarp

1 Answers

0
votes

Here's a recommendation of code:

Change the load to call the load of books in a separate method. Then, after save the same method will be called. Just replace the methods you have of the same name with these.

Private Sub frmSearchUpdateBooks_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Me.LoadBooks()

End Sub

Private Sub LoadBooks()
    Me.BooksTableAdapter.Fill(Me.DatabaseDataSet.Books)
    Me.DataGridView1.DataSource = Me.DatabaseDataSet.Books
End Sub

Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
    Try
        BooksBindingSource.EndEdit()
        BooksTableAdapter.Update(DatabaseDataSet)
        MsgBox("Successfully saved.", MsgBoxStyle.OkOnly, "Success!")
        Me.LoadBooks()

    Catch ex As Exception
        MsgBox("Please fill all the information needed. If all informations are filled up, please re-check the ISBN as some book might have the same ISBN as you are entering.", MsgBoxStyle.OkOnly, "Error!")
    End Try
End Sub