943,839 Members | Top Members by Rank

Ad:
  • VB.NET Discussion Thread
  • Unsolved
  • Views: 825
  • VB.NET RSS
Aug 3rd, 2009
0

Update database

Expand Post »
I connected a access database by usin codes, But when i add new data the data doesn't display in same time. it need to restart the programme. can I solve this?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tdapower is offline Offline
5 posts
since Jul 2009
Aug 3rd, 2009
0

Re: Update database

use refresh()
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
Nada_ward is offline Offline
53 posts
since Jul 2009
Aug 3rd, 2009
-1

Re: Update database

can i plz c the code
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009
Aug 3rd, 2009
0

Re: Update database

Welcome to DaniWeb tdapower! Please post the relevant code for your application on this thread so we can see where the problem is. when you post code be sure to use code tags:

[code=vb.net]
...code here
[/code]
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Aug 5th, 2009
0

Re: Update database

vb.net Syntax (Toggle Plain Text)
  1.  
  2. Imports System.Data.OleDb
  3. Public Class frmLecAdd
  4. Dim con As New OleDbConnection
  5. Dim ds As New DataSet
  6. Dim da As OleDb.OleDbDataAdapter
  7. Dim sql As String
  8.  
  9.  
  10. Dim rowNo As Integer
  11.  
  12.  
  13.  
  14. Private Sub pbUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbUpdate.Click
  15.  
  16.  
  17.  
  18. Dim cb As New OleDb.OleDbCommandBuilder(da)
  19. Dim mycmd As New OleDb.OleDbCommand
  20. Dim rows As Integer
  21.  
  22.  
  23. con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = database.mdb;"
  24. con.Open()
  25.  
  26. sql = "SELECT * FROM lec_info"
  27. da = New OleDb.OleDbDataAdapter(sql, con)
  28. da.Fill(ds, "myDB")
  29.  
  30.  
  31.  
  32. mycmd.CommandText = "INSERT INTO lec_info(lecturer_name,staff_no,course_name) VALUES('" & txtLname.Text & "','" & txtStaffNo.Text & "','" & cboCourses.Text & "')"
  33. mycmd.Connection = con
  34.  
  35. rows = mycmd.ExecuteNonQuery()
  36. con.Close()
  37. ds.AcceptChanges()
  38.  
  39. End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tdapower is offline Offline
5 posts
since Jul 2009
Aug 5th, 2009
0

Re: Update database

This is my code is
when i add new record using this i have to restart my programme to see the new data.
pls help me...

vb.net Syntax (Toggle Plain Text)
  1.  
  2. Imports System.Data.OleDb
  3. Public Class frmLecAdd
  4. Dim con As New OleDbConnection
  5. Dim ds As New DataSet
  6. Dim da As OleDb.OleDbDataAdapter
  7. Dim sql As String
  8.  
  9.  
  10. Dim rowNo As Integer
  11.  
  12.  
  13.  
  14. Private Sub pbUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles pbUpdate.Click
  15.  
  16.  
  17.  
  18. Dim cb As New OleDb.OleDbCommandBuilder(da)
  19. Dim mycmd As New OleDb.OleDbCommand
  20. Dim rows As Integer
  21.  
  22.  
  23. con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0; Data Source = database.mdb;"
  24. con.Open()
  25.  
  26. sql = "SELECT * FROM lec_info"
  27. da = New OleDb.OleDbDataAdapter(sql, con)
  28. da.Fill(ds, "myDB")
  29.  
  30.  
  31.  
  32. mycmd.CommandText = "INSERT INTO lec_info(lecturer_name,staff_no,course_name) VALUES('" & txtLname.Text & "','" & txtStaffNo.Text & "','" & cboCourses.Text & "')"
  33. mycmd.Connection = con
  34.  
  35. rows = mycmd.ExecuteNonQuery()
  36. con.Close()
  37. ds.AcceptChanges()
  38.  
  39. End Sub
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tdapower is offline Offline
5 posts
since Jul 2009
Aug 5th, 2009
0

Re: Update database

u have to update the data table
VB.NET Syntax (Toggle Plain Text)
  1. da.update(datatable)

u have used data adapter to retrieve data thn why dont u use it to insert as well

VB.NET Syntax (Toggle Plain Text)
  1. Dim drNewRow As DataRow = DataTable.NewRow()
  2. drNewRow("lecturer_name") = txtLname.Text
  3. drNewRow("staff_no") = txtStaffNo.Text
  4. drNewRow("course_name") = cboCourses.Text
  5.  
  6. DataTable.Rows.Add(drNewRow)
  7. da.Update(DataTable)
Last edited by babbu; Aug 5th, 2009 at 2:43 am.
Reputation Points: -1
Solved Threads: 23
Posting Whiz in Training
babbu is offline Offline
207 posts
since Jun 2009
Aug 9th, 2009
0

Re: Update database

I tryied your code, but that doesn't work well
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tdapower is offline Offline
5 posts
since Jul 2009
Aug 9th, 2009
0

Re: Update database

everything are seems fine just need to update dataset
VB.NET Syntax (Toggle Plain Text)
  1. rows = mycmd.ExecuteNonQuery()
  2. con.Close()
  3. ds.AcceptChanges()
  4. da.Update(ds)


should work fine.
Last edited by emint; Aug 9th, 2009 at 4:10 pm.
Reputation Points: 10
Solved Threads: 4
Junior Poster in Training
emint is offline Offline
60 posts
since Jun 2009
Aug 10th, 2009
0

@ tdapower

Tryout this code(It's bit different and i't tested):


Imports System.data
Imports System.Data.OleDb
Public Class frmLectureRecords
    Dim con As OleDbConnection
    Dim cmd As OleDbCommand()
    Dim da As OleDb.OleDbDataAdapter
    Dim ds As New DataSet()
    Dim Sql As String

    Private Sub frmLectureRecords_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        con = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Application.StartupPath & "\college.mdb;Persist Security Info=False")
        con.Open()
        Sql = "SELECT * FROM lec_info"
        da = New OleDb.OleDbDataAdapter(Sql, con)
        da.Fill(ds, "lec_info")
        con.Close()
        DataGridView1.ClearSelection()
        ' Set the DataGridView properties to bind it to our data...
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "lec_info"
        DataGridView1.Columns(0).HeaderText = "Lecturer Name"
        DataGridView1.Columns(1).HeaderText = "Staff No"
        DataGridView1.Columns(2).HeaderText = "Course"
    End Sub
End Class
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Vivek_1986 is offline Offline
11 posts
since May 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in VB.NET Forum Timeline: can't show a form...
Next Thread in VB.NET Forum Timeline: Same data gets appended repeatedly





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC