Results 1 to 6 of 6

Thread: How to refresh database after adding a new record?

  1. #1
    Join Date
    Jan 2009
    Posts
    36

    How to refresh database after adding a new record?

    Hi,

    I am working on vb.net project & i have added a record to my SQL database & now I want to refresh it but don't understand how to do this. Any help?

  2. #2
    Join Date
    May 2008
    Posts
    35

    Re: How to refresh database after adding a new record?

    If you set the database objects that you use to reference your database to nothing, and then whenever you want to have them refresh, just recreate the objects, and pass it another query. That should effectively load the new information.

  3. #3
    Join Date
    Jan 2009
    Posts
    44

    Re: How to refresh database after adding a new record?

    I have this code please have a look
    I hope this helps!

    Code:
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
      LoadDb()
    
    End Sub
    
    
    Private Sub LoadDb()
           Conn.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\AddressBook.mdb"
           Conn.Open()
    
           Sql = "Select * from tblContacts"
           da = New OleDb.OleDbDataAdapter(Sql, Conn)
    
           da.Fill(ds, "AddressBook")
    
           MaxRows = ds.Tables("AddressBook").Rows.Count <--- it doubles the count after adding a row
    
           Conn.Close()
    
    End Sub
    
    Private Sub cmdCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdCommit.Click
    
           If iRow <> -1 Then
               Dim cb As New OleDb.OleDbCommandBuilder(da)
               Dim dsNewRow As DataRow
    
               dsNewRow = ds.Tables("AddressBook").NewRow()
    
               dsNewRow.Item("FirstName") = txtFirstName.Text
               dsNewRow.Item("SurName") = txtSurname.Text
    
               ds.Tables("AddressBook").Rows.Add(dsNewRow)
               da.Update(ds, "AddressBook")
    
               MsgBox("New Record has been added to the Database")
           End If
    
           
           LoadDb()
    
    End Sub
    
    
    Private Sub cmdNextRow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNextRow.Click
    
           If ds.Tables("AddressBook").Rows.Count > 0 Then
               If iRow < ds.Tables("AddressBook").Rows.Count Then
                   NavigateRecords() ' display the record
                   iRow = iRow + 1
                   lblRecordNumber.Visible = True
                   lblRecordNumber.Text = "Record Number " & iRow & " of " & ds.Tables(0).Rows.Count
               Else
                   MsgBox("No More Rows")
               End If
           End If
    
    End Sub

  4. #4
    Join Date
    May 2008
    Posts
    44

    Re: How to refresh database after adding a new record?

    What is the actual database you are using?

    Another thing you can try is closing and reopening the dataset, e.g.
    MyADOQuery1.ExecSQL;
    MyADOTable1.Close;
    MyADOTable1.Open;

    The problem with opening and closing is getting your newly inserted record to appear as the selected record in the DBGrid after you re-open the dataset.
    To do that you could use the Locate method (MyADOTable1.Locate) (the syntax can be a little complicated, here's an UNTESTED example:
    MyADOTable1.Locate(['FieldName1', 'FieldName'2'], ['Smith', 'John'], []);
    But that is untested so it may be wrong - look in the help file if necessary.

  5. #5
    Join Date
    May 2008
    Posts
    63

    Re: How to refresh database after adding a new record?

    You could possibly use the FileSystemWatcher ... have an INSERT trigger on the SQL table that creates a text file in an otherwise empty folder, the FileSystemWatcher in VB recognises this new file and refreshes the datagrid, then deletes the file.

    There is probably an easier way but I can't think of it right now.

  6. #6
    Join Date
    May 2008
    Posts
    27

    Re: How to refresh database after adding a new record?

    • To add a record to the database, we invoke the AddNew method. The syntax for our example data control is:

    dtaExample.Recordset.AddNew

    This statement will blank out any bound data tools and move the current record to the end of the database. At this point, you enter the new values. When you move off of this record, the changes are automatically made to the database. Another way to update the database with the changes is via the Update method.

    After adding a record to a database, you should invoke the Refresh property of the data control to insure proper sorting (established by RecordSource SQL statement) of the new entry. The format is:

    dtaExample.Refresh

Similar Threads

  1. How to bring next record from database in asp.net?
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 02-02-2010, 05:38 PM
  2. Warning message when adding data to database
    By Jagavi in forum Software Development
    Replies: 5
    Last Post: 28-01-2010, 04:40 PM
  3. Adding Image in SQL database through vb.net application?
    By Sanket07 in forum Software Development
    Replies: 4
    Last Post: 16-02-2009, 06:41 PM
  4. Adding sud domain PTR record using DNSCMD?
    By Claude Lachapelle in forum Windows Server Help
    Replies: 3
    Last Post: 25-06-2008, 08:20 PM
  5. Windows DNS server - force A record to update PTR record
    By Peter Cumming in forum Windows Server Help
    Replies: 1
    Last Post: 27-05-2006, 07:00 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,867,910.70921 seconds with 17 queries