Solved

Saving data from observablecollection to database

Posted on 2011-09-19
10
2,299 Views
1 Endorsement
Last Modified: 2013-11-12
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#.
1
Comment
Question by:Masterabhi
10 Comments
 
LVL 29

Expert Comment

by:anarki_jimbel
ID: 36564234
0
 

Author Comment

by:Masterabhi
ID: 36568498
I am still not able to understand how I am going to save changes back to database from my observable collection.
0
 

Author Comment

by:Masterabhi
ID: 36574474
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 96

Expert Comment

by:Bob Learned
ID: 36577909
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
 

Author Comment

by:Masterabhi
ID: 36580714
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
 
LVL 96

Expert Comment

by:Bob Learned
ID: 36580775
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
 
LVL 4

Accepted Solution

by:
theHollow earned 500 total points
ID: 36593702
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
 

Author Closing Comment

by:Masterabhi
ID: 36958996
Thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Greetings C# program 17 124
What is "public object" in C#? 3 64
Switching Visual Studio 2015 to normal debug display 3 37
Adding items to a C# list incrementally 5 28
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

792 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