Hi, i would like to know how to refresh data base after i insert records. I explain, i have form in which user add data into database ACCESS, after that if he would like to view all data he got it but without the latest added, so i was obliged to close application and reopen it, this is not useful :(
i tried to use form.refresh() method but seems not to work with my purpose.
thanks in advance for help ;)

Dani AI

Generated

Form.Refresh only repaints the UI; it does not requery your Access database or reload the data source. After an INSERT (or UPDATE/DELETE), you need to repopulate whatever the grid/list is bound to, then notify the binding layer.

Typical WinForms pattern:

  • Keep a single DataTable and a BindingSource.
  • After saving, clear and refill that DataTable using the same SELECT you used at startup, then tell the BindingSource to reset.

Example (VB.NET, Access via OleDb):

' module-level
Private ReadOnly bs As New BindingSource()
Private ReadOnly dt As New DataTable()

Private Sub LoadRows()
    Using cn As New OleDb.OleDbConnection(connString),
          adp As New OleDb.OleDbDataAdapter("SELECT * FROM YourTable ORDER BY ID", cn)
        dt.Clear()          ' avoid duplicates
        adp.Fill(dt)        ' pull fresh rows
    End Using
    bs.DataSource = dt
    DataGridView1.DataSource = bs
    bs.ResetBindings(False) ' tell the UI to refresh from dt
End Sub

Private Sub SaveRow()
    Using cn As New OleDb.OleDbConnection(connString),
          cmd As New OleDb.OleDbCommand("INSERT INTO YourTable(Col1, Col2) VALUES(?,?)", cn)
        cmd.Parameters.AddWithValue("@p1", txtCol1.Text)
        cmd.Parameters.AddWithValue("@p2", txtCol2.Text)
        cn.Open()
        cmd.ExecuteNonQuery()
    End Using
    LoadRows()              ' requery after insert
End Sub

If this is ASP.NET Web Forms, the idea is the same but you call DataBind after refetching: run your SELECT again to get a fresh DataTable/List, set GridView.DataSource = freshData, then GridView.DataBind(). Also ensure you only bind on If Not IsPostBack Then ... at initial load, and explicitly rebind after an insert click.

Troubleshooting tips:

  • Reuse the same connection string and query for both initial load and refresh.
  • Always parameterize your INSERT to avoid data type surprises.
  • If rows still do not appear, verify you are writing to the same .mdb/.accdb file the app reads (relative paths can point to a copy in bin\Debug).

Recommended Answers

All 3 Replies

I am afraid that, you have to call that function / subroutine for database thing again.

Hi, what function/subroutine you talk about :)
what should my function do ?? close and open data base ?
thx in advance ;)

after i insert records. I explain, i have form in which user add data into database ACCESS, after that if he would like to view all data he got it but without the latest added, so i was obliged to close application and reopen it, this is not useful

You have mentioned that your user had insert records. Then, to only way is to use the function that gets the data again.

Lets say you start a program and automatically it shows the current data in Access, this getting the current data should be named as "Function A" (Actually for easy reference, you should call it GetData or whatever you deem appropirate).

Then your user went to insert / update records. This insert / update record has to be done in some other functions / subroutine in your code.

So now you have updated the database, but you cannot see the updated result, because the retrieved datagridview etc data controls is not synchronize with Access (Or your database such as SQL, for other's benefit.)

Hence, you should call that "Function A" you have loaded during the start of your application. In the case where you have put this section on Load Event / Public Sub New() part, you should extract this getting database's details onto another subroutine / function depending if that returns a value (Again, it is up to your preference, you may update your datagridview etc data controls within the subroutine, or return as a new dataset so that different functions can use it for different purpose.). Subroutine is used when NOT returning any value.

For your sake, i put an example code.

Public Sub New()
LoadDB()
End Sub
Public Sub LoadDB()
'Code for setting the connection string is omitted.
command.CommandText = "" 'Fill in the Access DB command text yourself in between of the 2 quotes. 
conn.Open()
Dim da As New OleAdapter(command)
Dim ds As New DataSet
da.Fill(ds,"YourTableName")
conn.Close
'Databind your datagridview / gridview etc data controls into this ds, or using this ds to load your combobox etc. This is out of scope.
End Sub
Public Sub Update()
'Code for setting the connection string is omitted.(You may declare "command" and "conn" as public shared variable, so that the connection string is only loaded in application's startup, in the New() subroutine. This will minimize repeating codes.
) 
command.CommandText = "update " 'Fill in the Access DB command text yourself in between of the 2 quotes and after "Update". (This is update database) 
conn.Open()
command.ExecuteNonQuery()
conn.Close
LoadDB()'Get the latest update again.
End Sub
Public Sub Insert()
'Code for setting the connection string is omitted. (You may declare "command" and "conn" as public shared variable, so that the connection string is only loaded in application's startup, in the New() subroutine. This will minimize repeating codes.
)

command.CommandText = "insert into " 'Fill in the Access DB command text yourself in between of the 2 quotes and after "insert into". (This is insert database) 
conn.Open()
command.ExecuteNonQuery()
conn.Close
LoadDB()'Get the latest update again.
End Sub
'The code for button click is omitted.
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.