0

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
Paul Williams
  • 1,554
  • 7
  • 40
  • 75

2 Answers2

1

The best approach is to add the record to the DataSource of the DataGrid. The DataGrid will automatically update. In this case its likely to be somehting like

HomeForm.MasterDataSet.Tournaments.AddTournamentsRow( CInt(SanctioningIDTxt.Text) , _
                           Trim(TournamentNameTxt.Text) ,  _
                           "1" , _
                           EventDateTimePck.Value, _
                           TableFirstNumberNo.Value,  _ 
                           GameList.SelectedIndex, _
                           FormatList.SelectedIndex, _
                           Convert.ToInt32(ToIDTxt.Text) )

As an aside you might want to consider using parameterized queries for your Insert statement

Conrad Frix
  • 51,984
  • 12
  • 96
  • 155
  • When you say parameterized queries, what do you mean? – Paul Williams Feb 24 '12 at 21:14
  • 1
    Instead of concatenating in your values you put in `?` as placeholders and then set the parameter values. this fixes problems like if your tournament name was "O'Brien's Cup" and also removes potential [SQL injection](http://xkcd.com/327/) attacks. Here's [an example](http://stackoverflow.com/questions/9401888/parameterized-query-for-inserting-values) of c# to Access. Sorry I couldn't find any VB.NET to access but its really not all that different. – Conrad Frix Feb 24 '12 at 21:24
  • When I use the code that you provided, I get an error: "Overload resolution fails because no accessible 'AddTournamentsRow' accept this number of arguments. EDIT: I just answered my own question. Evidently the MasterDataSet didn't have all these parameters in it's query. (Still new to making these.) – Paul Williams Feb 24 '12 at 23:12
0

This is my answer I hope you like it. On my project I have a switchboard and in that switchboard it contains the data grid view for my data. Before it only refresh it self when the user used the query and it will refresh it self. I taught about it how to do it different. So I used this technique the timer refresh effect. How does it work I put a timer and in the timer I copy and paste this code from the form load.

Me.ADDRESS_TICKETTableAdapter.Fill(Me.Database1DataSet.ADDRESS_TICKET)

And in the settings of the timer i turn it on and every 9 seconds it reloads the data you can change it that it reloads every 1 second if you like depends how many records it comes in. I hope this answer help you in anyway. It work for me.

NOE2270667
  • 55
  • 1
  • 4
  • 12
  • Do I have to use the timer? – Paul Williams Aug 12 '13 at 23:51
  • It depends on what do you want to accomplish Paul. I used a timer doing inputing that code in the timer and I choose how many seconds or minutes or hours I want it to refresh so my co-workers could see the new data – NOE2270667 Aug 13 '13 at 13:18