Click here to Skip to main content
15,887,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I can't get the Datagridview to update after an insert. The record in the table gets added but when I refresh the Datagridview it doesn't show new record. When I reload the form it is there.

I'm missing something and can't figure out what?

Thanks

What I have tried:

Private Sub CUDAreadings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'MubsDataSet.tblReadings' table. You can move, or remove it, as needed.

        UpdateDataGrid()

        ClearTextBoxes()
        tbReadMeterNo.Focus()
        btnAdd.Enabled = True
    End Sub



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

            Dim cm = New OleDb.OleDbCommand
            With cm
                .Connection = con  'dcMubs
                .CommandType = CommandType.Text

                .CommandText = "INSERT INTO tblReadings (ReadMeterNo, ReadCusNo, ReadDate, Reading, ReadUsage, " &
                                         "ReadType, ReadMethod, ReadTech, ReadPostDate) " &
                                        "VALUES (@ReadMeterNo, @ReadCusNo, @ReadDate, @Reading, @ReadUsage, " &
                                       "@ReadType, @ReadMethod, @ReadTech, @ReadPostDate) "
                cm.Parameters.Clear()

                cm.Parameters.AddWithValue("@ReadMeterNo", Me.tbReadMeterNo.Text)
                cm.Parameters.AddWithValue("@ReadCusNo", Me.tbReadCusNo.Text)
                cm.Parameters.AddWithValue("@ReadDate", Me.dtpReadDate.Value.Date)
                cm.Parameters.AddWithValue("@Reading", Me.tbReading.Text)
                cm.Parameters.AddWithValue("@ReadingUsage", Me.tbUnitsUsed.Text)
                cm.Parameters.AddWithValue("@ReadType", Me.tbReadType.Text)
                cm.Parameters.AddWithValue("@ReadMethod", "Manual")
                cm.Parameters.AddWithValue("@ReadTech", Me.tbReadTech.Text)
                cm.Parameters.AddWithValue("@ReadPostDate", DBNull.Value)

                cm.ExecuteNonQuery()

                'MsgBox("Reading Record UPDATED.", MsgBoxStyle.Information)
            End With

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            errornumber = 160 'Assigned error number for logging & debugging.
            clsLogFile = New LogFile(ex.Message, errornumber)
        End Try

        If tbReadType.Text <> lastReadType Then
            lastReadType = tbReadType.Text
        End If

        Call ClearTextBoxes()
        dtpReadDate.TabStop = False
        tbReadTech.TabStop = False
        tbReadMeterNo.Focus()
        UpdateDataGrid()

    End Sub

    Private Sub UpdateDataGrid()
        Try
            'clear out the datasource for the Grid view
            Me.DataGridView1.DataSource = Nothing
            'refill the table adapter from the dataset table 
            Me.TblReadingsTableAdapter.Fill(Me.MubsDataSet.tblReadings)
            'reset the datasource from the binding source
            Me.DataGridView1.DataSource = Me.TblReadingsBindingSource
            'should redraw with the new data
            Me.DataGridView1.Update()
            Me.DataGridView1.RefreshEdit()

        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Critical)
            errornumber = 161 'Assigned error number for logging & debugging.
            clsLogFile = New LogFile(ex.Message, errornumber)
        End Try

    End Sub
Posted
Comments
CHill60 26-Mar-21 9:36am    
Looks like Me.MubsDataSet is not being refreshed. So when you reload the form it is being "initialised" I guess? You need include that in your btnAdd_Click piece perhaps
bgcwaterman 26-Mar-21 12:29pm    
I added the following and it didn't work.

'clear the dataset before filling
Me.MubsDataSet.Clear() <--------added this line
'refill the table adapter from the dataset table
Me.TblReadingsTableAdapter.Fill(Me.MubsDataSet.tblReadings)
'clear out the datasource for the Grid view
Me.DataGridView1.DataSource = Nothing
'reset the datasource from the binding source
Me.DataGridView1.DataSource = Me.TblReadingsBindingSource
'should redraw with the new data
Me.DataGridView1.Update()
Me.DataGridView1.RefreshEdit()

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900