Does OnValidateEntityBeforeDelete behave the same as OnValidateEntityBeforeSave ?

Posts   
 
    
Posts: 5
Joined: 11-Sep-2006
# Posted on: 10-Oct-2006 17:49:22   

Upon myEntity.Delete(), I want to validate the data and return the result back to the UI if the validation fail. I did the following code and somehow the OnValidateEntityBeforeDelete() is not triggered.

myValidatorClass

Public Overrides Sub ValidateEntityBeforeDelete(ByVal involvedEntity As IEntityCore)

' Always throw this exception for testing purpose

Throw New ORMEntityValidationException("Custom Validate by JT", toValidate)

MyBase.ValidateEntityBeforeDelete(involvedEntity)

End Sub

myEntityPartialClass

Protected Overrides Function CreateValidator() As SD.LLBLGen.Pro.ORMSupportClasses.IValidator Return New CareMC.Template.ValidatorClasses.CrvlTemplatesSetValidator End Function

myUIEvent

Protected Sub btnDeleteSet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteSet.Click

    Dim oTemplateSet As New CrvlTemplatesSetEntity(cmbTemplateSet.SelectedValue.ToString)

    **_oTemplateSet.Delete()_**

End Sub

The code above doesn't auto trigger the ValidateEntityBeforeDelete() in the validator class. Do I need to explicitely call oTemplateSet.Validator.ValidateEntityBeforeDelete(oTemplateSet) before calling oTemplateSet.Delete() ?

What is the most common practice way of capturing the validation message and displaying it in the UI layer ? Should I do the Try/Catch ORMEntityValidationException at the UI or LLBL ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 10-Oct-2006 18:49:56   

Be sure to use the latest templates, this bug was fixed on July 13th. So please download the latest templates/tasks archive from the website's customer area. You're adviced to use the full installer to update the complete system to the latest version.

(with 'this bug' is meant the fact that OnValidateEntityBeforeDelete() wasn't called in selfservicing entities. This was a template error.)

Frans Bouma | Lead developer LLBLGen Pro
Jessynoo avatar
Jessynoo
Support Team
Posts: 296
Joined: 19-Aug-2004
# Posted on: 10-Oct-2006 19:28:53   

As for the message, you can also use the SetEntityError and SetEntityFieldError, which you can then retrieve through the IDataErrorInfo implementation.

Posts: 5
Joined: 11-Sep-2006
# Posted on: 10-Oct-2006 20:34:29   

Cool, it works now... Thanks for the tip guys.

This is how I plan to detect and capture the validation error message:

Protected Sub btnDeleteSet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDeleteSet.Click

    Dim oTemplateSet As New CrvlTemplatesSetEntity(cmbTemplateSet.SelectedValue.ToString)

    Try
        oTemplateSet.Delete()

    Catch ex As ORMEntityValidationException
        lblTemplateSetValidatorStatus.Text = ex.Message
    End Try

End Sub


What do you think ?

Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 11-Oct-2006 08:52:06   

Sounds good

AbbasMalik
User
Posts: 11
Joined: 11-Oct-2011
# Posted on: 04-Apr-2012 17:06:45   

What is wrong with the following code? adapter.FetchScalar throws error "Object reference not set ..."

   public override void ValidateEntityBeforeDelete(IEntityCore involvedEntity)
    {
        FormulaEntity toValidate = (FormulaEntity)involvedEntity;

        //Used QuerySpec API to check RI
        var qf = new QueryFactory();
        var qTarrif = qf.Create()
                        .Select(qf.Tariff.Where(TariffFields.FormulaId == toValidate.FormulaId).Any());

        using (var adapter = new DataAccessAdapter())
        {
            //Error: Object reference not set ...
            if **(adapter.FetchScalar<bool>(qTarrif)) **
                          throw new ORMException(EXISTS_TARIFF_MESSAGE);
        }
    }
Walaa avatar
Walaa
Support Team
Posts: 14946
Joined: 21-Aug-2005
# Posted on: 04-Apr-2012 18:19:49   

The code looks fine as far as I can see.

Could you please post the stack trace. And debug to see if any object is null at this linr of code.

AbbasMalik
User
Posts: 11
Joined: 11-Oct-2011
# Posted on: 05-Apr-2012 12:42:01   

at SD.LLBLGen.Pro.QuerySpec.MiscellaneousExtensionMethods.ToScalarValue[TValue](List`1 source) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\QuerySpec\ExtensionMethods\MiscellaneousExtensionMethods.cs:line 78

at SD.LLBLGen.Pro.QuerySpec.Adapter.AdapterExtensionMethods.FetchScalar[TValue](IDataAccessAdapter adapter, DynamicQuery query) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\QuerySpec\AdapterSpecific\AdapterExtensionMethods.cs:line 193

at ABC.Data.Extended.Validators.FormulaValidator.ValidateEntityBeforeDelete(IEntityCore involvedEntity) in D:\ABCDev\ABC.Data.Extended\Validators\FormulaValidator.cs:line 112

at SD.LLBLGen.Pro.ORMSupportClasses.EntityCore`1.OnValidateEntityBeforeDelete() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Core\EntityCore.cs:line 1664

at SD.LLBLGen.Pro.ORMSupportClasses.EntityCore`1.CallValidateEntityBeforeDelete() in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\Core\EntityCore.cs:line 575

at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.DeleteEntity(IEntity2 entityToDelete, IPredicateExpression deleteRestriction) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 2134

at SD.LLBLGen.Pro.ORMSupportClasses.DataAccessAdapterBase.DeleteEntity(IEntity2 entityToDelete) in c:\Myprojects\VS.NET Projects\LLBLGen Pro v3.5\Frameworks\LLBLGen Pro\RuntimeLibraries\ORMSupportClasses\AdapterSpecific\DataAccessAdapterBase.cs:line 2102

at ABC.UI.Maintenance.frmFormula.Delete() in D:\ABCDev\ABC.UI.Maintenance\Misc\frmFormula.cs:line 139

(why did you post it in this thread? -- Otis)

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39588
Joined: 17-Aug-2003
# Posted on: 05-Apr-2012 13:11:55   

1) please download the latest build of v3.5 2) please next time don't post in threads which are old, even though you might think the problem is related. Thanks.

Frans Bouma | Lead developer LLBLGen Pro
AbbasMalik
User
Posts: 11
Joined: 11-Oct-2011
# Posted on: 05-Apr-2012 19:17:44   

I have downloaded the latest build of 3.5 today but result is the same. Do I need to regenerate the source code?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Apr-2012 07:26:06   

I don't think you need to re-generate, but just in case, do it. I can't reproduce over here with similar code:

OrderEntity toValidate = new OrderEntity();
toValidate.ShipCountry = "USA";

//Used QuerySpec API to check RI
var qf = new QueryFactory();
var qTarrif = qf.Create()
                .Select(qf.Order.Where(OrderFields.ShipCountry == toValidate.ShipCountry).Any());

using (var adapter = new DataAccessAdapter())
{
    var exists = adapter.FetchScalar<bool>(qTarrif);
    Assert.IsTrue(exists);
}

If the problem persists, analyze the Generated SQL to know what is being generated. If not, please attach a tiny repro case (just LLBLGen project, repro code and the DB DDL , all zipped, without bins/dlls).

David Elizondo | LLBLGen Support Team
AbbasMalik
User
Posts: 11
Joined: 11-Oct-2011
# Posted on: 06-Apr-2012 14:00:37   

Here is the SQL captured from server side:

exec sp_executesql N'SELECT TOP(@p7) CASE WHEN CASE WHEN EXISTS (SELECT [LPA_L1].[Country] FROM (SELECT [TestDB].[dbo].[Country].[COUNTRY] AS [Country] FROM [TestDB].[dbo].[Country] WHERE ( ( [TestDB].[dbo].[Country].[COUNTRY] = @p1))) [LPA_L1]) THEN 1 ELSE 0 END=1 THEN @p3 ELSE @p5 END AS [LLBLV_1] FROM [TestDB].[dbo].[Country] ',N'@p1 nchar(10),@p3 bit,@p5 bit,@p7 bigint',@p1=N'ABC ',@p3=1,@p5=0,@p7=1

When the table is empty or truncated, above SQL returns 0 rows, and this is where FetchScalar throws exception. Otherwise above SQL returns 1 row and FetchScalar works as expected.

Can you check your code by truncating the table?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Apr-2012 21:50:00   

You are right, and that's because the generated sql assumes there are at lest 1 rows in your table. To overcome this use a Nullable<bool> (bool?) in your FetchScalar. Example:

OrderEntity toValidate = new OrderEntity();
toValidate.ShipCountry = "BBBBL";

//Used QuerySpec API to check RI
var qf = new QueryFactory();
var qTarrif = qf.Create()
                .Select(qf.Order.Where(OrderFields.ShipCountry == toValidate.ShipCountry).Any());

using (var adapter = new DataAccessAdapter())
{
    var rest = adapter.FetchScalar<bool?>(qTarrif);
    var exists = rest.HasValue && rest.Value;
    Assert.IsFalse(exists);
}
David Elizondo | LLBLGen Support Team
AbbasMalik
User
Posts: 11
Joined: 11-Oct-2011
# Posted on: 07-Apr-2012 08:54:15   

Thanks. It is ok to me if it is by design.