Solved

dataset acceptchanges vs bindingsource endedit

Posted on 2006-06-23
7
8,364 Views
Last Modified: 2012-08-13
are they the same?
0
Comment
Question by:newyuppie
  • 3
  • 3
7 Comments
 
LVL 17

Expert Comment

by:ZeonFlash
ID: 16974060
Technically, no.  When you call AcceptChanges, it invokes EndEdit automatically.  This might clear things up:  http://msdn2.microsoft.com/en-us/library/system.data.datarow.acceptchanges.aspx
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16974504
No, they're not the same and - with apologies to ZeonFlash ;-) - the difference is a bit more than technical.  

When a user edits data in a bound control those edits are not immediately (e.g. with each keystroke) passed to the datasource (ie the datatable): that only happens when there is notification that the editing has ended.  Soemtimes this happens implicitly - e.g. if the user moves to a different row/record - but if not the notification has to be given explicitly with .EndEdit.

When that notification has been given the edited data is passed to the datatable but (a) a version of the data in its unedited state is also retained by the datatable and (b) the .RowState flag is set to indicate that the row has been changed.  What .AcceptChanges does is remove earlier versions of all rows from the datatable (or from all datatables in the dataset) and reset all the .RowState flags.

The implications of this are important so far as updating the database is concerned.  TableAdapters (and DataAdapters) work, so far as their .Update commands are concerned, by checking the .RowState flags.  If .AcceptChanges has been called - resetting the flags - before .Update is called the adapter will find no flags indicating that any changes need to be made.  So it will do nothing.  The database will not be updated.

Roger
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16975842
roger thanks for this clarification. maybe you can instruct me on which cases should one call .acceptchanges, and on which cases .endedit? seems like if i need to update a database with a tableadapter i should not call .acceptchanges. maybe this call should only be used if you create the dataset programatically and dont need to persist that to a database.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 34

Accepted Solution

by:
Sancler earned 500 total points
ID: 16976139
As a rule of thumb .AcceptChanges should really only be necessary after an .Update has been called.  Before VB.NET 2005, Framework 2, this had to be done explicitly to ensure that the application's local datatable remained "in synch" with the equivalent database table, each regarding the same records as currently valid.  But in VB.NET 2005 DataAdapters (hence TableAdapters) have a new property which can be set to call .AcceptChanges automatically when an .Update is performed.  See this

http://msdn2.microsoft.com/en-us/library/system.data.common.dataadapter.acceptchangesduringupdate.aspx

and, for an explanation of the order in which things happen on an .Update, see this

http://msdn2.microsoft.com/en-us/library/system.data.common.dataadapter.update.aspx

about half way down the page.  Although there will no doubt be special cases in which the programmer will want to "custom-control" the calling of .AcceptChanges, I think for most practical purposes the sensible approach in VB.NET 2005 is to ensure that .AcceptChangesDuringUpdate is set to True and then forget about it.

If you create/fill a datatable/dataset programmatically then you can make your own rules about when to call .AcceptChanges.  As there is no need to keep the data "in synch" with anything, then there is perhaps no need to call it at all.  On the other hand, it does "tidy things up" a bit.  So the choice is yours.

As I said before, .EndEdit is often called implicitly (or automatically) when a user navigates to a new row/record: e.g. by clicking on a different row in a DataGridView or using a forward or back button on a BindingNavigator.  The one time it can be essential to call it explicitly is when a user can move directly from editing a record to a "Save" button.  It depends on precisely which controls are being used to edit the records but it is possible, in that scenario, that (because there has been no movement to a different record) the edit on the current record will not have got back to the datatable before the .Update is actioned.  For that reason, it is sensible to precede any .Update call with an .EndEdit on the BindingSource (or .EndCurrentEdit on a CurrencyManager or BindingManagerBase).  It does no harm if it is not in fact needed, but it avoids the possibility of the latest change not reaching the database.

Again, there may be scenarios where the programmer wants more detailed control (or to make assurance doubly sure) but as a rule of thumb that is the only place - just before an .Update - that I would bother about it.  The one possible exception to that rather blase approach is explained here

http://msdn2.microsoft.com/en-us/library/system.windows.forms.bindingsource.addnew.aspx

at the para. numbered 3 in the Remarks.  But that is quite a special case - as you will see if you follow the link to IEditableObject interface - and, for a "rule of thumb", I reckon it can be ignored ;-)

Roger
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16980179
roger, thank you so much for your detailed and most informative answer.
i have no questions, everything is perfectly explained and i understand it.
as a failry new ADO.NET user in vb2005, i had a hell of a time trying to understand all the events, methods, things about data binding. i have found your links very very useful, definitely what i needed.
am raising the points to 500

thanks again for the answer
0
 
LVL 13

Author Comment

by:newyuppie
ID: 16980181
raising points
0
 
LVL 34

Expert Comment

by:Sancler
ID: 16980267
Thanks for the points and your most gracious comments.

Roger
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Multiple file Upload asp.net 2 40
Setting runtime form location 4 31
More on Time zones in vb 2010 12 37
Duplicating a tab page content to another in TabControl control 11 41
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now