DataTable.AcceptChanges

A

Allan Bredahl

Hi All

I have a problem with getting a DataTable to submit changes back to the
database when calling AcceptChanges.


What I do is simplyfied this :


1) I set up a SqlDataAdapter with SelectCommand, InsertCommand,
UpdateCommand and DeleteCommand pointing to each a stored procedure.

Dim ad as new SqlDataAdapter
Dim ds as new DataSet


........ I setup the adapter 1) ........

ad.Fill(ds)

Dim dt as DataTable = ds.tables("MyTable")
Dim row as DataRow = dt.NewRow

row("field1") = "New String"
row("field2") = 234{int}
row("field3") = 212.34{single}

dt.Rows.Add(row)
dt.AcceptChanges()



This adds the new row to the DataTable just fine, but nothing happens in the
database when calling AcceptChanges.


Am I missing somethig completely here, or shouldn't I be able to insert the
new row into the database using AcceptChanges ??

Do I have to call ad.Update(ds) ??

This is not so easy since in my real code the DataAdapter and the actual
DataTable is placed/used in different places.


Does anyone have a solution to this ??

Thanks in advance

Allan
 
M

Miha Markic

Hi Allan,

AcceptChanges merely consolidates the changes once they are sucesfully
stored to database.
The right order would be:

Update(..)
AcceptChanges(..)
 
A

Allan Bredahl

That Means that I must have access to the DataAdapter if I want to be able
to commit the changes to the database ??


The problem is that I have a function that uses a DataAdapter to Fill my
DataSet, but I do not have access to this from where I need to commit the
changes to the DataSet/DataTable


thanks

Allan
 
C

Cor

Hi Allan,

If the dataadapter is not there, you can make a newone, it does not have to
be the same one.

And then exact as Miha wrote

xxxDataadpter.update(ds,"mytablename")

I hope this helps,

Cor
 
J

Jon Skeet [C# MVP]

Allan Bredahl said:
That Means that I must have access to the DataAdapter if I want to be able
to commit the changes to the database ??

Yes. The DataAdapter is the only thing that links the database and the
dataset.
The problem is that I have a function that uses a DataAdapter to Fill my
DataSet, but I do not have access to this from where I need to commit the
changes to the DataSet/DataTable

Then you need to change your design, I'm afraid.
 
A

Allan Bredahl

Hi Cor

I know that is a posibility, but I must try to find the best and most clean
way to do it.


Maby the best solution is to go the MS way and follow there Application
Architecture guidelines, and work with a DataAccessLayer


Allan
 
M

Miha Markic

Hi Allan,

As other pointed out, the dataadapter is correct way.
The DAL itself uses them behind the scenes.
 
J

Jay B. Harlow [MVP - Outlook]

Allan,
In addition to all the other comments
Am I missing somethig completely here, or shouldn't I be able to insert the
new row into the database using AcceptChanges ??
AcceptChanges changes the DataRow.RowState to Unchanged, it also updates the
DataRow Current & Original values.

http://msdn.microsoft.com/library/d...l/frlrfSystemDataDataRowVersionClassTopic.asp

The DataAdpater.Update method does not bother looking at Unchanged rows. The
DataAdapter.Update method is what actually updates the database. Note
DataAdapter.Update will call DataRow.AcceptChanges...

You may want to read David Sceppa's book "Microsoft ADO.NET - Core
Reference" from MS Press. It is a good tutorial on ADO.NET to learn the
nuances of when & where to call AcceptChanges as well as when & where to
create DataAdapters. It is also an excellent desk reference once you know
ADO.NET.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top