IDataStoreBase.Validate(Out List<ValidationResult> validationResults) Method
.NET Standard 2.x
Verifies the data using the data validation rules set in the DataStore, and outputs the verification results.
Namespace: DWNet.Data
Assembly: DWNet.Data.dll
Syntax
public bool Validate(out List<ValidationResult> validationResults);
Parameters
validationResults
System.Collections.Generic.List<System.ComponentModel.DataAnnotations.ValidationResult>
A list of validation results.
Returns
System.Boolean
Returns true
if all data meet the validation rule; false
if any data does not meet the validation rule.
Examples
The following code example specifies the validation rule (budget > 0), and then modifies one of the budget values to a negative value. The Validate
method will display the error message for the data that does not meet the rule.
using DWNet.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
namespace Appeon.ApiDoc.IDataStoreBaseExamples
{
public class ValidateExample
{
private readonly SchoolContext _context;
public ValidateExample(SchoolContext dataContext)
{
// Sets the data context
_context = dataContext;
}
public void Example2()
{
// Instantiates a DataStore object with datawindow: d_department.
var datastore = new DataStore("d_department", _context);
// Retrieves rows from the database for datastore.
datastore.Retrieve();
// Sets the validation rule with the third column: value for budget must be
// greater than 0.
datastore.SetValidate("budget", "budget > 0");
// Sets the vaue for budget in row 1 to -350000.
datastore.SetItem(0, "budget", -350000m);
// If all data passes validation, commit data to database,
// otherwise, display an error.
bool valid = datastore.Validate(out List<ValidationResult> results);
if (valid)
{
datastore.Update();
}
else
{
foreach (var validationResult in results)
{
Console.WriteLine("Error Message: {0}",
validationResult.ErrorMessage);
}
}
/*This code produces the following output:
Error Message: Row 0 column Budget data validation failed.
*/
}
}
}
Example Refer To
Model Class: D_Department
DataWindow File: d_department
Applies to
.NET Standard
2.x