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!