detailModel.EndEdit() change propreties to NULL

Oct 4, 2011 at 4:13 AM
Edited Oct 4, 2011 at 4:15 AM

Hello,

My child window have 2 comboboxs.  And after editing, those values become NULL.  After the BeginEdit, the values are OK.  After the EndEdit (whitout doing anything), the values are NULL.  Any idea?  For Add, everthing work fine.

This is the code (bypassing child window) :

            // Create a product detail model
            ProduitDetailViewModel detailModel =
                new ProduitDetailViewModel(viewModel.ProduitSelectionne);

            // Set categories
            detailModel.Categories = viewModel.Categories;
            detailModel.Fournisseurs = viewModel.Fournisseurs;
            // Start editing
            detailModel.BeginEdit();
            detailModel.EndEdit();

            // Show ProductDetail view
            //ProduitDetailView itemDetail = new ProduitDetailView(detailModel);
            //itemDetail.Closed += (s, ea) =>
            //{
            //    if (itemDetail.DialogResult == true)
            //    {
            //        // Confirm changes
            //        detailModel.EndEdit();
            //    }
            //    else
            //    {
            //        // Reject changes
            //        detailModel.CancelEdit();
            //    }
            //};
            //itemDetail.Show();

Oct 6, 2011 at 3:30 PM

Just clarifying....

Are you using Ria Services?

You mention 2 combo boxes that end up coming back nulled, but the above code they would never get assigned to a combobox?

If they are bound to the comboboxes (I assume Categories and Fournisseurs), in a 2 way binding, you might make sure that they are observable collections and bound to the selectedItem, otherwise you could be losing the value there.

Are you losing all your values in the above code or just the 2?

Just a couple thoughts and questions

Robert

Oct 6, 2011 at 7:55 PM

Yes RIA Services.

 

But I bypass the childwindow, so I don't use combobox.

For testing, I just use BeginEdit and EndEdit.  After the EndEdit, the two properties (related to two tables in my model) become Null.

It's seem related to the relation between my entity in my edmx file.  When I delete the relation, my property keep the original value! (but of course I cannot use the .Include in my query).

Oct 20, 2011 at 6:59 PM
Edited Oct 20, 2011 at 7:01 PM

Hi there,

I am having the exact same issue! In my particular Entity Model however I must note that I am bringing in a view and manually setting an association to it as a lookup table. When adding a new record using the same combobx binding I currently have it saves fine. However, when I edit a record in the child window and EndEdit() is called, this particular value is coming back NULL. I know the bindings are correct in the combobox because adding a new record is working just fine. Also, my other two comboboxes are saving without any issue, its only the one combobox that I have set my associations manually. I do have all the required Includes as well.

I think it may have something to do with the copy that is created when BeginEdit() is called not copying over the value of the combobox because of the manual association setting in the model?? Because this behavior is not occurring when adding a new record as no copy is created in that case. Is there any way to check what values the copy contains after BeginEdit() is called??

Any ideas??? Thanks!

Oct 26, 2011 at 2:12 PM

Curiously when I test with Norhtwind (for testing) everything is ok.  When I use my own database, this problem occurs.

I will very appreciate any help/clue/suggestion/recommandation about this issue.

Have nice day.

Coordinator
Nov 6, 2011 at 2:09 PM

Hi guys,

Sorry for my delay in responding to this thread -- been a tad hectic for me lately. :-)  I think Bacaret is on the right track.  Using BeginEdit and EndEdit may not be the right way to go when using the toolkit with WCF RIA Services, which has its own implementation of IEditableObject.  With WCF RIA Services, all you have to do is start changing properties on an entity, and the DomainContext will automatically track changes, so that they are persisted when you call SaveChanges on the DomainContext.  To add or remove entities, you call methods on the DomainContext.

It's important to be aware that some features of the toolkit (such as editing, validation) don't apply when using WCF RIA Services, because RIA provides those out of the box.

Cheers,

Tony

Nov 17, 2011 at 9:06 PM

Hi Everyone,

Just wanted to update this thread and say that I have discovered a solution to my issue, I hope it can help others as well. The issue was not actually with EndEdit() at all, it was actually in my ComboBox binding. Make sure that your using SelectedItem & NOT SelectedValue for you ComboBox binding. Do not bind SelectedValue to the ID Field, you want to use SelectedItem and bind to the associated entity. So for example you want to bind to Customer & not to CustomerID.

On a side note, as simple as it seems I realized this when I was trying to get validation notifications to work on my ComboBox. Since RIA metadata doesent let you use the [Required] attribute on Entity Properties, it complains that you need to set it on the Key field instead, which will not work with our ComboBox binding, I had to make CustomValidation for each of my ComboBox properties for validation, and wala I get the red box on my ComboBox when validation fails.

I hope this can help someone else. Tony I just wanted to say that your toolkit and videos have been an absolutely HUGE help for me to grasp the concept of MVVM and RIA Services. I really appreciate the hard work you have put into this wonderful toolkit and your videos are by far the best I've seen on the topic. It was so frustrating not being able to find any proper examples of how this stuff all bolts together before I found this toolkit.

Thanks!

Nov 18, 2011 at 12:15 AM
Edited Nov 18, 2011 at 12:18 AM

Hello Bacarat,

Thank a lot for sharing this precious information.  But for me, still the same problem.

I just comment the openning child window, (so there is no comboboxs)

        // Edit item
        private void editItemButton_Click(object sender, RoutedEventArgs e)
        {
            // Exit if no product selected
            if (viewModel.ProduitSelectionne == null) return;

            // Create a product detail model
            ProduitDetailViewModel detailModel =
                new ProduitDetailViewModel(viewModel.ProduitSelectionne);

            // Set categories
            detailModel.Categories = viewModel.Categories;
            detailModel.Fournisseurs = viewModel.Fournisseurs;

            // Start editing
            detailModel.BeginEdit();
            detailModel.EndEdit();  // *** AFTER THIS LINE, EVERY "FOREIGN KEY" TURNS TO NULL **************
            // Show ProductDetail view
            //ProduitDetailView itemDetail = new ProduitDetailView(detailModel);
            //itemDetail.Closed += (s, ea) =>
            //{
            //    if (itemDetail.DialogResult == true)
            //    {
            //        // Confirm changes
            //        detailModel.EndEdit();
            //    }
            //    else
            //    {
            //        // Reject changes
            //        detailModel.CancelEdit();
            //    }
            //};
            //itemDetail.Show();
        }

Curiously, when i use the database Northwind (the same of Tony), no problem! 

 

Coordinator
Nov 18, 2011 at 1:43 PM
Edited Nov 21, 2011 at 11:56 AM

I don't know if this relates, but in the Northwind database, CategoryId in the Products table is NULLABLE. That means you can have an orphan Product that does not have a Category. That may explain what you're seeing.

Cheers,
Tony

Nov 18, 2011 at 8:01 PM

@Louis475: I had the same problem...but I don't get a foreign key with "NULL" -> I always get the value "0" (this changes exactly after EndEdit()). I'm also working with WCF RIA (ADO.NET) and the reason was the EndEdit() Statement. Now it's working fine (as Tony described it some posts ago)...however I am thinking about how to add validation now?...

here is my code snippet:

            if (model.SelectedProduct == null) return;

            ProductViewModelDetail detailModel =
                new ProductViewModelDetail(model.SelectedProduct);

            detailModel.Category= model.Category;

            // Show ProductDetail view
            ProductDetailView itemDetail = new ProductDetailView(detailModel);
            itemDetail.Closed += (s, ea) =>
            {
                if (itemDetail.DialogResult == true)
                {
                    model.SaveChanges();
                }
            };
            itemDetail.Show();