It’s often useful to validate a field using common validation logic applied across a suite of pages rather than on individual pages. For example, you can validate a field by overriding the Validate() method in the code-behind file of individual pages, such as the Add Record and Edit Record pages. However, this results in code redundancy making it hard to maintain the code. To solve this, add your custom validation code in your application’s Business Layer.
The following examples illustrate two different ways to check if a field contains an empty string before saving a record to the database.
In your application’s Business Layer section are two classes containing the field you want to validate:
<Table Name>Table
<Table Name>Record
Customize the <Table Name>Record class to validate fields in a record. For example, to validate the Orders.ShipName field, modify:
…\Business Layer\Orders\OrdersRecord.cs (.vb)
To validate new records about to be inserted into the database, open <Table Name>Record.cs (.vb) file and add the following code:
C#:
public OrdersRecord()
{
this.InsertingRecord += new BaseClasses.IRecordWithTriggerEvents.InsertingRecordEventHandler(this.Insert);
}
/// <summary>
/// This custom method is an event handler for the insert event in the Data Access Layer.
/// </summary>
private void Insert(object sender, System.ComponentModel.CancelEventArgs e)
{
// You can modify the following sample code to fit specific need
if (this.ShipName == null || this.ShipName.Trim().Length == 0) {
throw new Exception("Ship name is empty ");
}
}
Visual Basic .NET:
<summary>
‘ This custom method is an event handler for the insert event in the Data Access Layer.
</summary>
Private Sub Insert(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.InsertingRecord
' You can modify the following sample code to fit specific need
If Me.ShipName Is Nothing OrElse Me.ShipName.Trim().Length = 0 Then
Throw New Exception("Ship name is empty")
End If
End Sub
If you want to add the same validation logic while editing a record in an edit page then, apply the above code customization to a method which handles UpdatingRecord event. Add the UpdatingRecord() event handler to the <Table Name>Record class in the Business Layer:
C#:
public OrdersRecord()
{
this.UpdatingRecord += new BaseClasses.IRecordWithTriggerEvents.InsertingRecordEventHandler(this.Update);
}
/// <summary>
/// This custom method is an event handler for the update event in the Data Access Layer.
/// </summary>
private void Update(object sender, System.ComponentModel.CancelEventArgs e)
{
// You can modify the following sample code to fit specific need
if (this.ShipName == null || this.ShipName.Trim().Length == 0) {
throw new Exception("Ship name is empty ");
}
}
Visual Basic .NET:
<summary>
' This custom method is an event handler for the update event in the Data Access Layer.
</summary>
Private Sub Update(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) _
Handles MyBase.UpdatingRecord
' You can modify the following sample code to fit specific need
If Me.ShipName Is Nothing OrElse Me.ShipName.Trim().Length = 0 Then
Throw New Exception("Ship name is empty")
End If
End Sub
Override the Record class’s Save() method to validate fields in a record. For example, to validate the Orders.ShipName field, modify:
…\Business Layer\Orders\OrdersRecord.cs (.vb)
C#:
/// This method is to override save() method to customize the save.
public override void Save()
{
//You can modify the following sample code to fit specific need
if (this.ShipName == null || this.ShipName.Trim().Length==0)
{
throw new Exception("Ship name is empty ");
}
base.Save();
}
Visual Basic .NET:
''' This method is to override save() method to customize the save.
Public Overrides Sub Save()
If Me.ShipName Is Nothing OrElse Me.ShipName.Trim().Length= 0 Then
Throw New Exception("Ship name is empty ")
End If
MyBase.Save()
End Sub
The Save() method is called when both adding and editing records, so only one customization is needed.
Part V: Customizing Application Code