Print Page | Close Window

Using Stored Proc in MVVM Light example

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2010
Forum Discription: For .NET 4.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4127
Printed Date: 12-May-2026 at 9:30pm


Topic: Using Stored Proc in MVVM Light example
Posted By: Delta_Rich
Subject: Using Stored Proc in MVVM Light example
Date Posted: 25-Apr-2013 at 4:24pm
I have watched and followed the video "MVVM Light with DevForce (Silverlight)", which was a great help. Now I am stuck with how to utilise the "Function Import" of a stored procedure returning a list of Entities.

If for example (from the example above) I create a Stored Proc that retrieves Customers where Country = parameter 1 and Region = parameter 2
(obviously this could be done using Linq but I want to show this as an example of parametised sprocs).
eg SQL would "EXECUTE usp_CustomerCtyReg 'USA', 'OR'"

In my dataservice I would try it this way - varying from the video example by calling the LoadCustomersViaSPQuery:

public void LoadCustomersViaSPQuery( Action<IEnumerable<Customer>> success = null, Action<Exception> fail = null)
{
     // Call the StoredProc for this
     Manager.RetrieveSPCustomersQuery(pCountry: "USA", pRegion: "OR")
          .ExecuteAsync(op =>
     {
          if (op.CompletedSuccessfully)
          {
               if (null != success)
               {
                    success(op.Results);
               }
          }
          else
          {
               if (null != fail)
               {
                    op.MarkErrorAsHandled();
                    fail(op.Error);
               }
          }
     });              
}



However, the success(op.Results) is erroring - "Delegate .... has some missing arguments."

Once I sought that out I imagine (and probably incorrectly) the ViewModel should just follow the remainder of the example.

Anyhow, can someone please provide me with the "how to" based on the video example?



Replies:
Posted By: kimj
Date Posted: 26-Apr-2013 at 10:42am
A StoredProcQuery when executed returns an IEnumerable, so if you cast the op.Results to an IEnumerable<Customer> this should work:

success(op.Results.Cast<Customer>());

You'll need a using statement for System.Linq too.


Posted By: Delta_Rich
Date Posted: 28-Apr-2013 at 8:15pm
Yep that got it.
Thanks



Print Page | Close Window