Saturday, March 17, 2012

Total and Subtotal: Creating Order Total Field

Let’s add a Total field to the Orders controller. This field will add Subtotal and Freight together to show the final cost of the whole order.

In the Project Explorer, select the Orders controller, and right-click on Fields. Press New Field.

Add New Field in 'Orders' Controller

Give this new field the following settings:

Property Value
Name Total
Type Currency
The value of this field is computed at run-time by SQL Expression
(select sum(unitprice*quantity*(1-discount)) 
from "order details" 
where "Order Details".OrderID = Orders.OrderID) + Orders.Freight
The value of this field is calculated by a business rule expression
CalculateOrderDetailsTotal(orderID) + freight
Label Total
Data Format String c
Values of this field cannot be edited True
Context Fields OrderDetails, Freight

New 'Total' Field properties, Part 1
New 'Total' Field properties, Part 2
New 'Total' Field properties, Part 3

Press OK to save this field.

We’ll need to bind this field to the data views. In the Project Designer, switch to Data Fields tab. On the action bar, press New | New Data Field. Use the following settings:

Properties Value
View editForm1
Category Orders

'Total' data field in 'editForm1' View

Press OK to save the data field. Double-click on Total field again, and create another data field with the following settings:

Properties Value
View grid1
Category N/A

'Total' data field in 'grid1' View

Save the data field, and regenerate the application. Navigate to the Order Form page, and select any order. You will see a Total field that sums up Subtotal and Freight.

New 'Total' field in the Order Form of the web application

No comments:

You can find more about Code OnTime Generator, Data Aquarium Framework, and other great products here.

© 2010 Code OnTime LLC. Intelligent code generation software for ASP.NET. Visit us at