views:

85

answers:

3

Hi, I would like to know if its possible to discard changes of only one record of only one table in datacontext.

I use databind to bind my controls on a form. I modify one record at a time. after the modification, the user have to hit save button to validate. But he can hit cancel. I would like that the cancel button discard all the changes that the user has done. Is it possible?

Ju

+2  A: 

If you don't call .SubmitChanges on the DataContext then the changes will not be applied to the database.

It's all about the DataContext when considering the change tracking of your entities.

If you would like to clear all changes, then close the datacontext and open a new one.

SteadyEddi
Yes but th eproblem is not the insert in the DB. The problem is that i wnat when you hit Cancel Button all changes is discarded.
Garcia Julien
I know that the ChangeSet can be retrieved by using DataContext.GetChangeSet, but I am not sure if the deletes, updates or inserts can be modified. In your situation I would look at options for applying any changes outside of the linq entities and only applying them when the changes are actually required to be made.
SteadyEddi
+2  A: 

Make it so your Dialog is a dumb UI. I.e, it handles all of the input of the data, but doesn't persist any data. So if you are using a dialog called from a form - have something like

If (myDlg.ShowDialog() = DialogResult.Ok)
     MyDataContext.SubmitChanges()
End If

This way, if your dialog doesn't return Ok (i.e. if they click cancel, the DataContext wont persist the results bak to the database.

This makes for reusable code as well.

Ben
Hi it's a good way i didn't know befor but my data is not in a dialog but in a user control. So thanks
Garcia Julien
A: 

Thanks for your response, I found a solution, i reload the datacontext when i hit cancel. It's working and it's faster. Thanks

Garcia Julien