0
votes

i am developing a sample app using the link below.

http://code.msdn.microsoft.com/Getting-Started-WCF-RIA-1469cbe2.

Its a silverlight business application using wcf ria services as mentioned in the link.

in the sample, while adding a new employee,if the employee 'title' field is unique in database and user try to enter duplicate title,how can i prevent it? i mean if one enters two same 'title' it should validate properly. How can i do this in the current scenario? i am adding new employee using a child window...

can any one help?

1

1 Answers

0
votes

You can solve your problem using one CustomValidation, and i think its the best way since you may or may not have all records loaded on the client side, and in the case you dont have all employes loaded you need to go to the DB to check if there is one with the title beeing inputed...

Basically it would go like this:

First Create the Class that's going to be responsible for the Employees Validation

public class EmployeeValidationRules
{
}

then the method that will actually do the Validation:

public static ValidationResult ValidateTitle(string title, ValidationContext context)
{
    AdventureWorks_DataEntities objectContext = new AdventureWorks_DataEntities();

    Employee employee = context.ObjectInstance as Employee ;
    if (objectContext.Emplyees.Any(e => e.Title == employee.Title))
    {
        return new ValidationResult("Title Allready Exists", new string[] { "Title" });
    }

    return ValidationResult.Success;
}

On the metadata generated by your domainservices in the Employee class search for the title property and you have to add this tag:

[CustomValidation(typeof(EmployeeValidationRules), "ValidateTitle")]     
public string Title{ get; set; } 

Note: All this must be done on the server side...

You can find more information about WCF Validations here:

http://www.silverlightshow.net/items/WCF-RIA-Services-Part-6-Validating-Data.aspx

Here is another very well written article by Jeff Handley on WCF validations:

http://jeffhandley.com/archive/2010/09/25/RiaServicesCustomValidationMethods.aspx