Saving data from observablecollection to database

I have a WPF datagrid in my UI that is bound to the observablecollection in my viewModel, which I had converted from DataSet (coming from the Model).
I need to know how can I implement the  Delete, Update, and Add operations and save the changes in the database. I have a separate button-events for all these operations.
I am using MVVM pattern.
Please provide code in C#.
MasterabhiAsked:
Who is Participating?
 
theHollowConnect With a Mentor Commented:
To clear something up:
You are not able to post anything back to the database automatically from an ObservableCollection.
That is not the way an ObservableCollection works.
An ObservableCollection is simply a list/enumerable-like type which also implements change notification events (for Binding/UI purposes).

In order to post anything back to the source, you will need to do in reverse what you did to get the data into the collection in the first place.

If you like automation, you may follow the good recommendations from TheLearnedOne and look into ORM models, like Entity Framework, Nhibernate or Linq. That will do the trick.
0
 
MasterabhiAuthor Commented:
I am still not able to understand how I am going to save changes back to database from my observable collection.
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
MasterabhiAuthor Commented:
I am also trying to make changes using, udatesourcetrigger=explicit in order to make changes when I hit save button. How should I bind save button to those changes?What should I write in Save command button? and
Please help. Thanks.
0
 
Bob LearnedCommented:
Are you using anything like Entity Framework, NHibernate, or LINQ-to-SQL?  If not, that might be a better choice, rather than translating from classes to a ADO.NET classic structures.
0
 
MasterabhiAuthor Commented:
No I am not using Entity Framework, Nhibernate or Linq to sql.
I am not able to get the Modified observable collection to save into the database.
0
 
Bob LearnedCommented:
I believe that WPF works a lot better with a ORM system, rather than classic ADO.NET classes, since they don't have to go through any conversion to/from the database, and would keep track of changes.  Otherwise, you would need to keep track of object state, and then make sure that the DataSet state is correct when updating back to database.  I can see so many reasons why that would be a problem.
0
 
MasterabhiAuthor Commented:
Thanks
0
All Courses

From novice to tech pro — start learning today.