?
Solved

dataset acceptchanges vs bindingsource endedit

Posted on 2006-06-23
7
Medium Priority
?
8,557 Views
Last Modified: 2012-08-13
are they the same?
0
Comment
Question by:newyuppie
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Accepted Solution

by:
Sancler earned 2000 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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

765 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