locked
Update Other Tables? RRS feed

  • Question

  • I have a couple project coming up and while I will not be developing the production version in LS, I would like to attempt them on my own time so I can learn LS.

    So in my purchase order system I want to record the number of items received to the OrderItemsTable, which is done when the user presses the built in save button. But then I want to also add the items to my InventoryTransactions table, and then for each item received I want to add the total received to the InventoryMaster as an update of the current on-hand count.  How do I do that is LS? 

    I assume the answer to this will apply to my second process.   I have students fill out an online application that saves everything to one or two tables.  Then I want to create a UI in LS that allows staff to review those items, clean up data as needed, and then if they mark the item to be imported to our production system it will have to perform a lookup to see what the next ID is, update it by one, and then move the fields from the intake table to the 25-28 tables in the ERP system. 

    Not sure how to update one or more table, I am sure it has to be in the Save event for my screen and I really would prefer not to write a trigger at the database level but that is an option.    How to I code the code-behind to execute all the queries and updates in C# 

    I have no Silverlight background yet. I have strong SQL Skills... Thanks in advance.

     


    Thank you!
    Saturday, December 11, 2010 3:40 AM

Answers

  • Not sure how to update one or more table, I am sure it has to be in the Save event for my screen and I really would prefer not to write a trigger at the database level but that is an option.    How to I code the code-behind to execute all the queries and updates in C# 

    LightSwitch makes this very easy for you to do -- we have server side methods that are called based on what you have submitted to the database.

    If you are in the entity designer for your "OrderItemsTable", if you select "Write Code", you will see "OrderItemsTable_Inserting".  This method will get run when a new OrderItems is being created and saved to the database.  There are similar methods for Updating and Deleting, which run when you update this entity or delete this entity.

    There are also corresponding methods for InsertED, DeletED, and UpdatED, which are run just after the rows are committed to the database.  These can be handy if you want to defer some logic until you know the new database-assigned IDs of the modified rows, or if you want to defer this processing until you know the first stage of the transaction will commit.

    So in your OrderItemsTable_Inserting method, here you can make the appropriate modifications to the InventoryTransactions table, and also do the updates to the on-hand count.  And all of these modifications will transact together in one atomic transaction (if they are all in the same data store); you should not need to explicitly call save at all.

    Play around with the various entity _inserting and _updating methods -- create and update other entities, etc.  See what happens when you cause an exception in these methods, etc.  I beleive you'll find the server programming model quite powerful.

     

    Tuesday, December 14, 2010 7:27 PM

All replies

  • Not sure how to update one or more table, I am sure it has to be in the Save event for my screen and I really would prefer not to write a trigger at the database level but that is an option.    How to I code the code-behind to execute all the queries and updates in C# 

    LightSwitch makes this very easy for you to do -- we have server side methods that are called based on what you have submitted to the database.

    If you are in the entity designer for your "OrderItemsTable", if you select "Write Code", you will see "OrderItemsTable_Inserting".  This method will get run when a new OrderItems is being created and saved to the database.  There are similar methods for Updating and Deleting, which run when you update this entity or delete this entity.

    There are also corresponding methods for InsertED, DeletED, and UpdatED, which are run just after the rows are committed to the database.  These can be handy if you want to defer some logic until you know the new database-assigned IDs of the modified rows, or if you want to defer this processing until you know the first stage of the transaction will commit.

    So in your OrderItemsTable_Inserting method, here you can make the appropriate modifications to the InventoryTransactions table, and also do the updates to the on-hand count.  And all of these modifications will transact together in one atomic transaction (if they are all in the same data store); you should not need to explicitly call save at all.

    Play around with the various entity _inserting and _updating methods -- create and update other entities, etc.  See what happens when you cause an exception in these methods, etc.  I beleive you'll find the server programming model quite powerful.

     

    Tuesday, December 14, 2010 7:27 PM
  • Matt  Thanks for the answer.  That is the direction I went but couldn't quite figure out how call entites updates, passing in the values I wanted to update.  I think I saw some place  that we can't call Stored Procedures.

    Do you know if there is any vedio, or blog posting showing how to do this in C#?      


    Thank you!
    Wednesday, December 15, 2010 10:37 PM
  • You may be able to use stored procedures using it in a WCF RIA Service.

    mysorian
    Wednesday, December 15, 2010 10:49 PM
  • Just watched my first introduction to WCF and RIA http://www.silverlight.net/learn/videos/all/net-ria-services-intro/   that is alot of learning curve on top of LS. Maybe it is best to learn SL first?  

    I did like the final product but clearly I have a large mountain to clime...


    Thank you!
    Saturday, December 18, 2010 6:52 PM