Data verification when saving with Lightswitch

148 Views Asked by At

Right now I have a table I'm entering in spud date and release date. I want to verify that when adding a new row it looks in that current table to see if there is any other spud dates, to make sure the new spud date is later then the previous spud and also to verify that it has a release date before it adds a new row. Anyone know how to do this.

if (this.ReleaseDate < SpudDate)
{
    results.AddPropertyError("Release Date cannot be before Spud Date");
}

I have this code in the validate function, but that's only when entering the current item. I also need to go back and view the other records to compare as well like I said.

     partial void ReleaseDate_Validate(EntityValidationResultsBuilder results)
      {
     IDataServiceQueryable<tblWellRigJunction> orders = (from o in  this.DataWorkspace.RigStatusData.tblWellRigJunctions
                                                            orderby o.ReleaseDate
                                                            where WellID.Equals(this.WellID)
                                                            select o).Take(1);

  foreach (tblWellRigJunction ord in orders)
        {

            if (ord.ReleaseDate.ToString() == "")
            {
                results.AddPropertyError("Previous Release Date entered is still null");
                return;
            }

            int result = DateTime.Compare(ord.SpudDate, this.SpudDate);

            if (result > 0)
            {
                results.AddPropertyError("Previous Spud Date is greater");
                return;
            }
        } 
1

There are 1 best solutions below

6
On

You could create a simple query that takes all records in the table and sorts them by the spud date. If you sort the spud date by descending, you can then compare to the .FirstOrDefault().

Spud Date Query

if (SpudDate < this.MySpudDates.FirstOrDefualt().SpudDate)
{
    results.AddPropertyError("New Spud Date cannot be before Previous Spud Date");
}