Friday, October 26, 2012
Order Form Sample–Part 21

Let’s add a Subtotal field to the order. This field will sum up the extended prices of all order details.

In the Project Explorer, switch to Controllers tab. Right-click on Orders / Fields, and press New Field.

New Field context menu option for Orders data controller.

The new field’s settings will be:

Property Value
Field Name Subtotal
Type Currency
The value of this field is computed at run-time by SQL Expression True
SQL
select sum(UnitPrice*Quantity*(1-Discount)) 
from "Order Details"
where "Order Details".OrderID = Orders.OrderID
Label Subtotal
Values of this field cannot be edited True
Data Format String c

Press OK to save the field.

Your application will incorporate this SQL Formula in the SELECT statements composed at runtime. This statement will look similar to:

select *, 
   (
   select sum(UnitPrice*Quantity*(1-Discount)) from "Order Details"
   where "Order Details".OrderID = Orders.OrderID
   ) Subtotal
from 
   "Orders" Orders

This is the output produced when the statement is executed in SQL Management Studio. The actual statement will look more complex, and may include user-defined sorting, filtering, and paging parameters.

Simplified statement of 'Subtotal' SQL Formula calculation in SQL Management Studio

We’ll need to bind this field to views, so that the end user can see the field value.

Drop Orders / Fields / Subtotal field node onto Orders / Views / grid1 node.

Dropping Subtotal field node onto 'grid1' view node.     Subtotal data field node has been created in 'grid1'.

Drop Subtotal field node onto Orders / Views / editForm1 node.

Dropping Subtotal field node onto 'editForm1' view node.     Subtotal data field node has been created in 'editForm1'.

Drop Subtotal field node onto Orders / Views / createForm1 view node.

Dropping Subtotal field node onto 'createForm1' view node.    Subtotal data field node has been created in 'createForm1'.

Regenerate the application, and select any order on the Order Form page. You will see the Subtotal field showing the sum of extended prices.

Subtotal data field is present in editForm1 of Orders.