adapter doesn't get last inserted record in access table

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Stardust
    New Member
    • May 2008
    • 1

    #1

    adapter doesn't get last inserted record in access table

    Hello everyone!

    I am new to VB.NET so please accept my apologies if the question is too easy. I created a concise code to explain the problem I have.

    I have an Access database that contains Table1 with 3 fields (IdCol=PK, Col1 and Col2). I create a datatable with the same structure, add 2 records, insert them in Table1 using a dataadapter. Problem is that if I create a new adapter to select records from Table1, the new dataset has one record less than the first dataset. Here is the code:

    Code:
            Dim dt As New DataTable("DTable")
            dt.Columns.Add("IdCol", GetType(Integer))
            dt.Columns.Add("Col1", GetType(String))
            dt.Columns.Add("Col2", GetType(String))
    
            Dim Row1 As DataRow
            Row1 = dt.NewRow()
            Row1("IdCol") = 1
            Row1("Col1") = "a1"
            Row1("Col2") = "b1"
            dt.Rows.Add(Row1)
    
            Row1 = dt.NewRow()
            Row1("IdCol") = 2
            Row1("Col1") = "a2"
            Row1("Col2") = "b2"
            dt.Rows.Add(Row1)
    
            Dim ds As New DataSet
            ds.Tables.Add(dt)
            Dim pk(0) As DataColumn
            pk(0) = ds.Tables(0).Columns("IdCol")
            ds.Tables(0).PrimaryKey = pk
    
            Dim cmd As New OleDbCommand, da As New OleDbDataAdapter
            cmd.Connection = con
            cmd.CommandText = "select * from Table1"
            cmd.CommandType = CommandType.Text
            da.SelectCommand = cmd
            da.Fill(ds, "DTable")
    
            Dim cb As New OleDbCommandBuilder(da)
            da.Update(ds, "DTable")
    
            Dim da2 As New OleDbDataAdapter, ds2 As New DataSet
            da2 = New OleDbDataAdapter("SELECT * FROM Table1", con.ConnectionString)
            da2.Fill(ds2, "Table1")
    So, ds has 2 records and ds2 has one record. What am I doing wrong? I noticed that if I place the line
    Messagebox.Show (ds.Tables(0).R ows.Count)
    after da.Update, ds2 will have 2 records as it should.

    Thank you in advance!
Working...