Validation at the business logic layer in Visual Studio.NET

By: Juan Carlos (John Charles) Olamendy Turruellas

Introduction

Validation is a very important feature of every enterprise application to ensure data consistency and error handling. Ensuring data consistency requires a combination of data input validation and protection of the message between the different layers of the application, that is, the messages that go from the presentation layer to the business logic layer which, in turn, go to the data access layer. In this article, I will explain how to implement validation at the business logic layer and use this feature in the presentation layer. Implementation validation at the business logic has benefits such as the definition of the validation at a single point in the application, so it’s very easy to maintain.

Implementation using Visual Studio.NET

To illustrate this technique, I’m going to use the AdventureWorks database shipped with the installation of SQL Server 2005. Let’s create a new solution in Visual Studio.NET and add two projects: one Windows application as the front-end and a class library for hosting the components of the business logic layer.

Add a strongly-typed DataSet to manage the data in the Production.Product table (see Figure 1).

Figure 1

Then, add a table adapter to the data set with the following query (see Listing 1).

select *

from Production.Product;

Listing 1

The result definition of the strongly typed data set is shown in Figure 2.

Figure 2

There is a problem if you display product data using DataGridView or other Windows control. When the user tries to enter a NULL value for a NOT NULL column such as the primary key ProductID or the column Name, then an exception is raised which can be handled using the appropriate events of the controls. In order to solve this problem, one strategy is to add a validation rule in the DataTable.

For example, we’re going to validate that the value of the column Name is not null as well as the range value for the ListPrice column is between 0 and 100.

Now double-click over the Product table in order to create the underlying ProductDataTable partial class. Then add an override method for OnTableNewRow and ColumnChanged methods to handle the validation as well as to add two methods one for validation of the column Name and the other for the ListPrice column (see Listing 2).

 

namespace BizLogicValidationPkg {

 

partial class DSProducts

{

partial class ProductDataTable

{

private void prvValidateNameColumn(DSProducts.ProductRow rowProduct)

{

if (rowProduct != null)

{

if (rowProduct.IsNull(this.NameColumn))

{

rowProduct.SetColumnError(this.NameColumn, “Product name is required”);

}

else

{

rowProduct.SetColumnError(this.NameColumn, “”);

}

}

}

 

private void prvValidateListPrice(DSProducts.ProductRow rowProduct)

{

if (rowProduct != null)

{

if (rowProduct.IsNull(this.ListPriceColumn))

{

rowProduct.SetColumnError(this.ListPriceColumn, “Product list price is required”);

}

else

{

if ((rowProduct.ListPrice >= 0) && (rowProduct.ListPrice <= 100))

{

rowProduct.SetColumnError(this.ListPriceColumn, “”);

}

else

{

rowProduct.SetColumnError(this.ListPriceColumn, “Product list price value is between 0 and 100”);

}

}

}

}

 

protected override void OnTableNewRow(System.Data.DataTableNewRowEventArgs e)

{

this.prvValidateNameColumn(e.Row as DSProducts.ProductRow);

this.prvValidateListPrice(e.Row as DSProducts.ProductRow);

 

base.OnTableNewRow(e);

}

 

protected override void OnColumnChanged(System.Data.DataColumnChangeEventArgs e)

{

if (e.Column == this.NameColumn)

{

this.prvValidateNameColumn(e.Row as DSProducts.ProductRow);

}

 

if (e.Column == this.ListPriceColumn)

{

this.prvValidateListPrice(e.Row as DSProducts.ProductRow);

}

 

base.OnColumnChanged(e);

}

}

}

}

Listing 2

Let’s compile the class library and add a reference to it in the Windows project. Then add an Object Data Source to the underlying strongly-typed data set as well as the code to load the product data from the DataGridView to the underlying data source (see Listing 3).

private void dfrmProductGrid_Load(object sender, EventArgs e)

{

ProductTableAdapter taProduct = new ProductTableAdapter();

taProduct.Fill(this.dSProducts.Product);

}

Listing 3

If you enter an invalid value for list price column, an error icon then appears (see Figure 3).

Figure 3

In the case of the Name column, we also have to handle the DataError event on the DataGridView (see Listing 4).

private void productDataGridView_DataError(object sender, DataGridViewDataErrorEventArgs e)

{

string strMessage = String.Format(“Data Error\nMessage is:{0}”,e.Exception.Message);

System.Windows.Forms.MessageBox.Show(strMessage, “Application Error“,MessageBoxButtons.OK,MessageBoxIcon.Error);

}

Listing 4

When you run the application and try to enter a null value for the Name column, then you will receive the following message box and the error icon then appears in the row (see Figure 4).

Figure 4

Now we need to a code to check whether to save the values or not (see Listing 5).

private void m_btnSave_Click(object sender, EventArgs e)

{

if (this.dSProducts.Product.HasErrors())

{

System.Windows.Forms.MessageBox.Show(“Please, check the errors in the Product DataGridView before saving data.”, “Application Error”, MessageBoxButtons.OK, MessageBoxIcon.Error);

}

else

{

ProductTableAdapter taProduct = new ProductTableAdapter();

taProduct.Update(this.dSProducts.Product);

}

}

Listing 5

And now run the application and see the results (see Figure 5).

Figure 5

The same technique can be applied to the Detail View of the product table.

Conclusion

In this article, I’ve explained how to implement validation at the business logic layer and use this feature in the presentation layer.

Advertisements

4 thoughts on “Validation at the business logic layer in Visual Studio.NET

  1. What’s Going down i’m new to this, I stumbled upon this
    I have discovered It absolutely useful and it has aided me out loads.

    I am hoping to contribute & help other customers
    like its aided me. Great job.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s