hi everyone kindly help me with my code, my form isn't showing my newly added records in the database but when i close it and then open again it will show. I want to know how to add refresh button and it will automatically refresh itself..


Thanks in advance!

Dani AI

Generated

A common cause is that the grid is still showing the in-memory result set that was loaded when the form opened, so newly INSERTed rows in the database don’t appear until the form re-queries the file on disk. That matches what called “rebind” and what suggested (timer); avoid polling with a timer because it causes flicker as warned. A cleaner pattern is: centralize the load logic in one method, bind the grid to a BindingSource, and call that method after any successful insert (or from a Refresh button).

Example pattern (keeps connection details in one place and uses a DataReader -> DataTable load so it’s distinct from earlier examples):

Private bsPersonnel As New BindingSource()

Private Sub LoadPersonnel()
    Dim connStr As String = GetConnectionString() 'keep connection in one helper
    Dim dt As New DataTable()
    Using cn As New OleDb.OleDbConnection(connStr)
        cn.Open()
        Using cmd As New OleDb.OleDbCommand("SELECT * FROM Personnel ORDER BY PersonnelID", cn)
            Using rdr As OleDb.OleDbDataReader = cmd.ExecuteReader()
                dt.Load(rdr)
            End Using
        End Using
    End Using
    bsPersonnel.DataSource = dt
    dataGridPersonnel.DataSource = bsPersonnel
End Sub

Private Sub btnRefresh_Click(...) Handles btnRefresh.Click
    LoadPersonnel()
End Sub

For automatic refresh after an insert from a separate dialog, raise an event from the insert form or call the parent’s LoadPersonnel directly. Example flow: child form raises RecordAdded; parent subscribes and calls LoadPersonnel when the event fires. Troubleshooting checklist: confirm the INSERT actually committed (open the Access file with Access or run a COUNT query), avoid only calling DataGridView.Refresh (it repaints UI only), clear the existing DataTable before refill if reusing it, and keep connection/adapter usage inside Using blocks to avoid file locks.

Recommended Answers

All 4 Replies

how are you showing your database records on your form?
you may use a timer tick option for refreshing page after a specified amount of time and fill your form again from the database.
show some of your code so that someone can help you better.

if you are using a gridview just get the data from the db and rebind the gridview as you do when you open the form the first time.
refreshing the whole page(although i use it) gives an awful flickering that u can't get rid of.

Declare these stuffs first:

Dim Conn As New OleDbConnection
    Dim Cmd As New OleDbCommand()
    Dim da As New OleDbDataAdapter(Cmd)
    Dim dt As New DataTable
    Dim ds As New DataSet

Here's how I display my database records using datagridview

Private Sub AdminConsole_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Cmd = New OleDbCommand("SELECT * FROM staffs", New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source =KDE.mdb;"))
        Me.da = New OleDbDataAdapter(Me.Cmd)
        Me.ds = New DataSet()

        Me.Cmd.Connection.Open()
        Me.da.Fill(Me.ds)
        Me.Cmd.Connection.Close()

        Me.dataGridAdmin.DataSource = Me.ds.Tables(0).DefaultView
    End Sub

And here's the code for my Refresh Button.

Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        Me.Cmd = New OleDbCommand("SELECT * FROM staffs", New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; Data Source =KDE.mdb;"))
        Me.da = New OleDbDataAdapter(Me.Cmd)
        Me.ds = New DataSet()

        Me.Cmd.Connection.Open()
        Me.da.Fill(Me.ds)
        Me.Cmd.Connection.Close()

        Me.dataGridAdmin.DataSource = Me.ds.Tables(0).DefaultView
    End Sub

hi how about this code of mine?

this is my declaration

Dim inc As Integer
    Dim con As New OleDb.OleDbConnection
    Dim cmd As New OleDb.OleDbCommand()
    Dim dbProvider As String
    Dim dbSource As String
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String
    Dim MaxRows As Integer

and ...

dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbSource = "Data Source = C:\Documents and Settings\JIM\My Documents\Visual Studio 2010\Projects\hrisbyith\hrisbyith\PersonnelQMDB.mdb"

        con.ConnectionString = dbProvider & dbSource

        con.Open()
        sql = "SELECT * FROM Personnel"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "Perinfo")

        con.Close()

can u make the refresh button for me?

tnx

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.