SIGN UP MEMBER LOGIN:    
ARTICLE

Validation in Linq to SQL and ASP.NET

Posted by John Charles Olamendy Articles | LINQ with C# May 18, 2010
In this article, I will show how to add business rules to Linq to SQL data model default set of schemas.
Reader Level:

Introduction

One important aspect of every enterprise application is the validation of business rules. In this article, I will show how to add business rules to Linq to SQL data model default set of schemas. When we define a LINQ to SQL data model, we will automatically have a default set of schema based validation constraints added to our data model classes.  For example, if you try to set a null value for a required column, then the LINQ to SQL data model will raise an error to ensure our database integrity is maintained. Although, it's very common to add additional business rules and application-level validation to our data model classes, and this is the case that I will explain in my article.

Getting started with the solution

For this example, we're going to use the Department entity whose instances' state will be stored in the dbo.Department table in an SQL Server database. The schema for this table is shown in the Listing 1.

CREATE TABLE [dbo].[Department](
          [DeptID] [int] NOT NULL,
          [DeptName] [varchar](50) NOT NULL,
          [Location] [varchar](50) NOT NULL,
 CONSTRAINT [PK_Department] PRIMARY KEY CLUSTERED
(
          [DeptID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
GO

Listing 1

Now, let's see where to implement the business rules of the enterprise application. Adding the business rules in the UI layer of our application means that the rule will be specific to only that one place, and this will not be automatically enforced when we add another UI to update the departments. So, the solution is to place the business logic validation in the SQL data model classes.

In order to illustrate this example, we're going to create a Web site solution in Visual Studio.NET 2008 and then add a Linq to SQL artifact inside it. Finally, drag and drop the Department table inside the Linq to SQL designer (see Figure 1).

1.gif
 
Figure 1

If you look inside the generated code for the Linq to SQL designer, all classes are defined as partial and you can see the code generated for the Department class.  So in order to call validation methods that implement the custom validation logic, we have to override the OnValidate method of the Department partial class.

Now inside the OnValidate method, we have to code the validation logic. Let's suppose in our example that we don't want to have departments from NY. So, the validation code is shown in the Listing 2.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

/// <summary>
/// Summary description for Department
/// </summary>
public partial class Department
{
    partial void OnValidate(System.Data.Linq.ChangeAction action)
    {
        if (this.Location.Equals("NY"))
        {
            throw new ArgumentException("Location can't be equal to NY");
        }
    }
}

Listing 2

Once we add the above validation code into the OnValidate method of the Department class definition, then the business rule will be enforced anytime we try to modify the database.

Next step is to show the validation error in a friend way to the users. In this case, we're going to use LinqDataSource control's mechanism for such a purpose, so we do not need to-do anything to our UI tier in order for this validation to occur - it will automatically be applied anywhere and everywhere our LINQ to SQL data model is used.

Next step is to go to the Default.aspx page. Let's add an error panel (see Listing 3).

<span id="spErrorPanel">
<asp:Literal id="ltrErrorMessage" runat="server" />
</span>

Listing 3

Next step is to add LinqDataSource control and configure it to the previously created Linq to SQL data model. (see Figure 2).

2.gif
 
Figure 2

You need to active the Enable Update option as well in the LinqDataSource (see Figure 3).

3.gif
 
Figure 3

Finally, let's add a GridView control to the page and link the GridView control to the LinqDataSource control.

By default when a user uses our GridView control to enter a non-valid value, the LINQ to SQL data model classes will raise an exception and the LinqDataSource control will in turn catch this error and provides an event that users can use to handle it. If the error is not handled, then it will be passed up to the Page to handle, and so on to the global Application_Error() event handler in the Global.asax file.

In our application, we're going to manage any update errors by handling the RowUpdated event on our GridView. This event will get fired every time an update is attempted on our LinqDataSource, and we can access the exception error details if the update event fails.

The code for the RowUpdated event is shown in the Listing 4.

protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)
{
    if (e.Exception != null)
    {
        this.ltrErrorMessage.Text = e.Exception.Message;
        e.ExceptionHandled = true;
        e.KeepInEditMode = true;
    }
    else
    {
        this.ltrErrorMessage.Text = "";
    }
}

Listing 4

Conclusion

In this article, I've shown show how to add business rules to Linq to SQL data model default set of schemas. Now you can add your own business rules to your data model.

Login to add your contents and source code to this article
share this article :
post comment
 

Thanks John Charles Olamendi it's really helpful.

Posted by Manish Singh Nov 21, 2011

I have custom validation in my datacontext that if a username is already existing, it won't let another user save with the same name. That works fine when I add a new user but I am having a problem updating the same user as it validates again and finds the user existing so it throws an exception. Please let me know how can I apply validation for only certain actions like only when I add but not on update. I am using the submitchanges() method and I don't know where can I differentiate whether it's an update or adding new record. Please help me . Thanks.

Posted by sukumar thota Jun 01, 2010
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor