This seems like a reasonable task, but Lightswitch does not make it easy. Just to help you along, here are a few tips:
- First, this becomes much easier when you write your own RIA Service or OData source and you assume full control over your entities.
- When using Lightswitch out-of-the-box, there is a command-pattern 'hack' to accomplish this that is recognized in the forums as an established pattern.
The short overview is: you define a custom entity (or table) in Lightswitch. It can represent about anything.
If your sp takes parameters, often the recommendation is to create columns for the parameters.
The idea is that you have a command button or such that gets invoked. In the MyButton_Execute()
override, you insert values into the table (You call workspace.ApplicationData.MyCustomTable.AddNew()
, set the values, and call Save_Changes()
).
Then you intercept the save pipeline: the MyCustomTable_Inserting(MyCustomTable entity)
method. Here, you write your ado.net code to invoke the sp. And the hack is then to call this.Details.DiscardChanges()
...you aren't really adding a new row...you are just using the pipeline to get access to a point where you can inject custom ado code.
Now, displaying the results is a bit trickier. Usually, the idea is that your sp updated some underlying tables, and if you refresh your screen, the updated data will show in the controls for those related tables.
To capture and display the actual results of your sp, you will have to experiment. Perhaps your screen displays the 'MyCustomTable' as a collection, and so you capture the sp output to that same entity, if it is returning a result set. Or if it returns a single row, maybe you can manually update some screen parameters that are linked to text boxes....as I said, Lightswitch does not make it easy, because it is not tracking the invocation of the sp as a table entity that has been modified. Also be aware that if you try to update your screen after the ado invocation has completed, you will be on a different thread. Probably your best bet is to have a table in your actual database that receives the output of the sp, and then have Lightswitch simply treat that results table as any other. You may need to add some sort of identifier so that you can filter the current screen to display only the sp results of the invocation specific to that screen. Best of luck.