This looks like an easy fix but I can't figure this out. I'm trying to have button click event on a subForm (NewTournament), add a record to a database, and then to notify a data grid which lists the records from the same database automatically (the grid is listed on HomeForm).
I so for am able to update the database and call upon a new window. But I can't get it to refresh that datagrid for anything. I know I'm supposed to clear the datagrid, get changes, then refill the grid. But everytime I do this, the code is NOT updating. Furthermore, I can very well see that the record is being added.
Private Sub CreateTournament_Click(sender As System.Object, e As System.EventArgs) Handles CreateTournament.Click
' Check the form for errors, if none exist.
' Create the tournament in the database, add the values where needed. Close the form when done.
Dim cn As OleDbConnection
Dim cmd, cmd1 As OleDbCommand
Dim icount As Integer
Dim str, str1 As String
Try
cn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='C:\Users\Paul Williams III\Documents\Visual Studio 2010\Projects\Everything OP Client\Everything OP Client\Master.mdb'")
cn.Open()
str = "insert into Tournaments (SanctioningID,TournamentName,TournamentVenue,TournamentDateTime,TournamentFirstTable,Game,Format,OrganizerID) values(" _
& CInt(SanctioningIDTxt.Text) & ",'" & Trim(TournamentNameTxt.Text) & "','" & _
"1" & "','" & EventDateTimePck.Value & "','" & TableFirstNumberNo.Value & "','" & GameList.SelectedIndex & "','" & FormatList.SelectedIndex & "','" & Convert.ToInt32(ToIDTxt.Text) & "')"
'string stores the command and CInt is used to convert number to string
cmd = New OleDbCommand(str, cn)
str1 = "select @@Identity"
icount = cmd.ExecuteNonQuery
cmd1 = New OleDbCommand(str1, cn)
Counter = CInt(cmd1.ExecuteScalar)
MessageBox.Show(Counter & " was the last inserted id")
'displays number of records inserted
HomeForm.MasterDataSet.Clear()
HomeForm.MasterDataSet.GetChanges()
HomeForm.TournamentsTableAdapter.Fill(HomeForm.MasterDataSet.Tournaments)
HomeForm.DataGridView1.Refresh()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Me.Close()
Dim n As New TournamentWindow
n.TournID = Counter
n.Show(HomeForm)
End Sub