Hi!

I just started using LightSwitch and i am amazed how great this tool is! However, i worked with php up until a week ago so im really new in vb programming language.

I started making an application for warehouse management and got stuck with reducing part quantity after placed sales order...

I found one post which gave me a piece of code that i could use. I adopted it to my needs and it looked like this:

Private Sub DeductInventory_Execute()
        ' Write your code here.
        For Each orddetail As OrderDetail In Me.OrderDetails
            Dim currentproduct = Me.DataWorkspace.ApplicationData.Parts.Where(Function(o) o.Id = orddetail.Part.Id).FirstOrDefault()
            If currentproduct IsNot Nothing Then
                currentproduct.Quantity = (currentproduct.Quantity - orddetail.Quantity)
            Else
                Throw New Exception("Not enough quantity")
            End If
        Next
        Me.Save()
    End Sub

The problem is that this code works on a button but i dont want users to press the button to deduct quantity in stock before placing the order. It should be automatically after the order is placed.

Than i got one suggestion to put it on order detail inserting event, which i did and now it looks like this:

Private Sub OrderDetails_Inserting(entity As OrderDetail)
            For Each orddetail As OrderDetail In Me.OrderDetails
                Dim currentproduct = Me.DataWorkspace.ApplicationData.Parts.Where(Function(o) o.Id = orddetail.Part.Id).FirstOrDefault()
                If currentproduct IsNot Nothing Then
                    currentproduct.Quantity = (currentproduct.Quantity - orddetail.Quantity)
                Else
                    Throw New Exception("Not enough quantity")
                End If
            Next
        End Sub

Now the problem is that when i put in the order detail quantity and save it, it takes quantity from all other orders that were placed for that product and just adds this new quantity on top of it each time new order is placed... Order detail is in the relation with Sales order as one sales order many order details. Tried to put it also on Sales order inserting but nothing happens.

Thank you for your help!

It's kind of hard to tell without the rest of your code, which is probably too big to post. But, one thing I noticed in the OrderDetails_Inserting sub routine your passing OrderDetail as a parameter but your code doesn't use it.

Hi! Thank you for your reply!

I am really new in this so im not writing my own long scripts. Im using visual studio 2012 LightSwitch to make some simple stock management system. And i dont understand where should i use subroutine in my code?

Namespace LightSwitchApplication

    Public Class ApplicationDataService

        Private Sub OrderDetails_Inserting(entity As o)
            For Each orddetail As OrderDetail In Me.OrderDetails
                Dim currentproduct = Me.DataWorkspace.ApplicationData.Parts.Where(Function(o) o.Id = orddetail.Part.Id).FirstOrDefault()
                If currentproduct IsNot Nothing Then
                    currentproduct.Quantity = (currentproduct.Quantity - orddetail.Quantity)
                Else

                End If
            Next
        End Sub
    End Class

End Namespace

Because this is all i have of the code that i use...

I tried putting it on particulare page view and there the code works but if i want to control insert, update and delete quantity i have to put it on the script which is more near to database (server) than to client side... But there it doesnt work. Gives me an error:

Unable to save data. Non-static method requires a target.

If you could help me with that i would be very greatfull... I was searching answer on internet but no luck...

This article has been dead for over six months. Start a new discussion instead.