What is wrong in this AD0.Net row update?

  • Thread starter Thread starter Gabriel Langen
  • Start date Start date
G

Gabriel Langen

Hi,

Here is a test code! Very simple and short!
The modify of row(0) doen't work but the add.row is OK!!!

I think, all the needed imports are OK!

Thanks
Imports System.Data

Imports System.Data.OleDb


Dim ds_Test As DataSet
Dim da_adapter As OleDbDataAdapter

Dim str_sql As String = "select * from liste_publications order by titre"

Dim str_conn As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=c:\crisp.mdb"

Dim dr_test As DataRow

ds_Test = New DataSet("DS_Test")

da_adapter = New OleDbDataAdapter(str_sql, str_conn)

Dim SQLBuilder As OleDbCommandBuilder = New OleDbCommandBuilder(da_adapter)

Randomize()

da_adapter.Fill(ds_Test, "Table_1")

' Modify of row(0) : Not OK

ds_Test.Tables("Table_1").Rows(0)("Titre") = " Titre xxxx"

Dim x As String

If ds_Test.HasChanges Then

' Message is OK

msgbox("Changed")

ds_Test.Tables("Table_1").AcceptChanges()

da_adapter.Update(ds_Test, "Table_1")

End If

Dim ajout As Boolean

ajout = True

' add of two rows (OK)

If ajout Then

MsgBox(ds_Test.Tables("Table_1").TableName)

dr_test = ds_Test.Tables("Table_1").NewRow

dr_test("cle") = CStr(Int(Rnd() * 10000))

dr_test("auteur") = "Auteur"

dr_test("titre") = " Titres"

ds_Test.Tables("Table_1").Rows.Add(dr_test)

dr_test = ds_Test.Tables("Table_1").NewRow

dr_test("cle") = CStr(Int(Rnd() * 10000))

dr_test("auteur") = "Auteur"

dr_test("titre") = " Titres"

ds_Test.Tables("Table_1").Rows.Add(dr_test)

da_adapter.Update(ds_Test, "Table_1")

End If

da_adapter.Dispose()

ds_Test.Dispose()

End
 
Hi Gabriel,

When you look in this newsgroup, you will see that a lot of people use the
acceptchanges for someting that it is not made for.

Acceptchanges is for:
to set the dataset that all updates are correct processed.
ds_Test.Tables("Table_1").AcceptChanges()
da_adapter.Update(ds_Test, "Table_1")

So with this sentence there is nothing to update anymore,
You only have the acceptchanges row, because the update does (if every thing
is correct) that himself with a full dataset.

I hope this helps?

Cor
 
HI Gabriel,

Just don't call AcceptChanges *before* Update and everything will be fine :)
 
Move the dataset AcceptChanges call to after the data adaptor Update
call.

Calling AcceptChanges of the dateset causes all the rowstate
indicators to be reset so when Update is called it checks the data
tables rowstates and as they are all unmodifed no updates take place.
 
Hi

I think your problem is here
If ds_Test.HasChanges Then

' Message is OK

msgbox("Changed")

ds_Test.Tables("Table_1").AcceptChanges() <--------- you should not accept changes. see explanation below

da_adapter.Update(ds_Test, "Table_1")

End If


if you accept the changes , the data adapter flags the lines has no
changes.
this means that the data adapter cannot send the changes to the
database, because the row is not marked with changes.

remove that line and it should be fine.
 
Gabriel,
Have you tried David Sceppa's book "Microsoft ADO.NET - Core Reference" from
MS Press?

I find David's book to be both a good tutorial on ADO.NET plus a good desk
reference once you know ADO.NET!

Hope this helps
Jay
 
Back
Top