Partager via


Business Apps Example for Silverlight 3 RTM and .NET RIA Services July Update: Comments about Adding…

A couple of people mentioned they are having problems getting adding to work in my example.    I think there are two issues that are getting people.  See the full series

 

1. You need to hit submit after you add.. Adding only adds to the local collection, to push it to the database, you need to call submit changes.   If you’d like to have Add.. always add it to the database, simply add a call to SubmitChanges(). (line 8).  But notice this does do a network operation..

    1: void addNewWindow_Closed(object sender, EventArgs e)
    2: {
    3:     var win = sender as AddNewWindow;
    4:     var context = dds.DomainContext as SuperEmployeeDomainContext;
    5:     if (win.DialogResult == true)
    6:     {
    7:         context.SuperEmployees.Add(win.NewEmployee);
    8:         context.SubmitChanges();
    9:     }
   10: }

 

2. You need to set Issues to some number greater than 100.  Otherwise it will add it to the database, but it gets filtered out when the list is refreshed.   Look at this code from the DomainService.. see line 4? 

    1: public IQueryable<SuperEmployee> GetSuperEmployees()
    2: {
    3:     return this.Context.SuperEmployeeSet
    4:                .Where(emp=>emp.Issues>100)
    5:                .OrderBy(emp=>emp.EmployeeID);
    6: }

Really, I guess I should hint that to the user by setting a min value for issues in the add dialog.  You can do that by changing the range attribute in the SuperEmployeesDomainService.metadata.cs file. 

    1: [Range(100, 10000,
    2:     ErrorMessage = "Issues must be between 100 and 1000")]
    3: public Nullable<int> Issues;

 

Related a common question has been how to write code that get’s called *after* the initial load of data is done.  Mostly RIA Services’ programming model avoids you having to deal with it, but sometimes there is no other way.  So here is a simple example of how to run some code after a load operation is complete.

    1: var context = dds.DomainContext as SuperEmployeeDomainContext;
    2: originFilterBox.ItemsSource = context.Origins;
    3: context.Load(context.GetOriginsQuery(), (lo) => 
    4: { 
    5:     //just to show you how to load..  
    6:     new ErrorWindow("Loaded.."+lo.Entities.Count()).Show(); 
    7: },null);

Hope this helps!  I have updated the demo files

Enjoy!

Comments

  • Anonymous
    September 07, 2009
    I have been picking uptips from your example and wondering in a future post if you could show splitting this in to the new RIA Class library, so we have an idea on what changes are required.

  • Anonymous
    September 07, 2009
    I should have looked a bit further before posting, I now found your part 13 which covers the class library for RIA But I will ask now, is there an index or table of contents that points to all the parts of your RIA series?

  • Anonymous
    September 07, 2009
    I agree it would be gret to auto gen a index page into a ria site.

  • Anonymous
    September 07, 2009
    In case we have additional column added into Database table or add new Table into database. How to refresh the whole data service without loosing the additional code that added manual into data services?

  • Anonymous
    September 07, 2009
    I'm going to really stretch this out for you Brad...  :) I'd like to see you build this example with the window, etc... using a MVVM approach.   RIA.NET with a MVVM will be an approach many will take.  ie. I've been thinking of using SilverlightFX with RIA.NET

  • Anonymous
    September 07, 2009
    Hi, Thanks for providing these tips. Thani

  • Anonymous
    October 24, 2009
    I am new to RIA Services. There are a lot of great samples and tips in this series (going already for almost 9 month) But I did not find Silverlight 3  Accordion sample with Hierarchical data. How to show Categories in Accordion headers and relevant Products in content using, for example, Northwind Database,???