Thursday, November 29, 2012
Calculating Read-Only Fields with C# and Visual Basic

Let’s create a calculated field in the Employees controller that will combine First Name and Last Name fields.

Start the Project Designer. In the Project Explorer, switch to the Controllers tab. Right-click on Employees / Fields node, and press New Field option.

New Field context menu option for Employees controller in Code On Time web application generator.

Assign the following properties:

Property Value
Name FullName
Type String
Length 80
Allow Null Values true
The value is calculated by a business rule expression true
Label Full Name
Values of this field cannot be edited true

Press OK to save.

In the Project Explorer, drag Employees / Fields / FullName field node and drop it onto Employees / Views / createForm1 to add a data field.

Dropping FullName field node onto 'createForm1' view.     FullName data field created in 'createForm1' view.

Right-click on Employees / Fields / FullName node, and press Add Code Converter.

Add Code Converter context menu option on FullName field node in the Project Designer.

Double-click on Employees / Fields / FullName node, and change the Context Fields property:

Property Value
Context Fields FirstName, LastName

On the toolbar, press Browse to generate the web application and code business rule.

Right-click on Employees / Business Rules / FullName_Convertor and press Edit Rule in Visual Studio.

Edit Rule in Visual Studio context menu option for 'FullName_Converter' business rule node.

The business rule file will open in Visual Studio. Replace the default code with the following:

C#:

using System;
using MyCompany.Data;

namespace MyCompany.Rules
{
    public partial class EmployeesBusinessRules : MyCompany.Data.BusinessRules
    {
        
        /// <summary>
        /// Rule "FullName_Converter" implementation:
        /// This method will execute in any view before an action
        /// with a command name that matches "Calculate|Insert|Update".
        /// </summary>
        [Rule("r100")]
        public void r100Implementation(
                    int? employeeID, 
                    string lastName, 
                    string firstName, 
                    string title, 
                    string titleOfCourtesy, 
                    DateTime? birthDate, 
                    DateTime? hireDate, 
                    string address, 
                    string city, 
                    string region, 
                    string postalCode, 
                    string country, 
                    string homePhone, 
                    string extension, 
                    string notes, 
                    int? reportsTo, 
                    string reportsToLastName, 
                    string photoPath, 
                    string fullName)
        {
            if (!(String.IsNullOrEmpty(firstName)) && !(String.IsNullOrEmpty(lastName)))
                UpdateFieldValue("FullName", firstName + ' ' + lastName);
        }
    }
}

Visual Basic:

Imports MyCompany.Data
Imports System

Namespace MyCompany.Rules
    
    Partial Public Class EmployeesBusinessRules
        Inherits MyCompany.Data.BusinessRules
        
        ''' <summary>
        ''' Rule "FullName_Converter" implementation:
        ''' This method will execute in any view before an action
        ''' with a command name that matches "Calculate|Insert|Update".
        ''' </summary>
        <Rule("r100")>  _
        Public Sub r100Implementation( _
                    ByVal employeeID As Nullable(Of Integer),  _
                    ByVal lastName As String,  _
                    ByVal firstName As String,  _
                    ByVal title As String,  _
                    ByVal titleOfCourtesy As String,  _
                    ByVal birthDate As Nullable(Of DateTime),  _
                    ByVal hireDate As Nullable(Of DateTime),  _
                    ByVal address As String,  _
                    ByVal city As String,  _
                    ByVal region As String,  _
                    ByVal postalCode As String,  _
                    ByVal country As String,  _
                    ByVal homePhone As String,  _
                    ByVal extension As String,  _
                    ByVal notes As String,  _
                    ByVal reportsTo As Nullable(Of Integer),  _
                    ByVal reportsToLastName As String,  _
                    ByVal photoPath As String,  _
                    ByVal fullName As String)
            If (Not (String.IsNullOrEmpty(firstName)) AndAlso Not (String.IsNullOrEmpty(lastName))) Then
                UpdateFieldValue("FullName", firstName + " " + lastName)
            End If
        End Sub
    End Class
End Namespace

Save the file.

Switch back to the web application generator. On the toolbar, press Browse.

Navigate to the Employees page, and create a new employee. Type a value for Last Name and First Name.

Typing in First and Last Name into each respetive field on the New Employees form.

The Full Name field will be updated when the user presses Tab or shifts focus away from the field.

Tabbing away from First Name will update the Full Name calculated field.