CSLA 2.1 custom validation help. Wierd exceptions

CSLA 2.1 custom validation help. Wierd exceptions

Old forum URL: forums.lhotka.net/forums/t/1586.aspx


sune42 posted on Wednesday, October 25, 2006

   
Hi

I today got some wierd validation exceptions:

Here's a few of them


System.NullReferenceException: Object reference not set to an instance of an object.
at Csla.Validation.ValidationRules.CheckRules()

Exception: DataPortal.Fetch failed (System.NullReferenceException: Object reference not set to an instance of an object.
at Csla.Validation.ValidationRules.CheckRules()

System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at Csla.Validation.ValidationRules.CheckRules(List`1 list)
   at Csla.Validation.ValidationRules.CheckRules()

Exception: DataPortal.Fetch failed (System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
   at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
   at System.Collections.Generic.List`1.Enumerator.MoveNext()
   at Csla.Validation.ValidationRules.CheckRules(List`1 list)
   at Csla.Validation.ValidationRules.CheckRules()

 

I have added two custom validation rules in


private void AddCommonRules()
{
 ...
    ...
            ValidationRules.AddRule(CommonRules.StringRequired, "Website");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs("Website", 100));
            ValidationRules.AddRule<CSLA_Listing>(ValidateURL, "Website");

            //
            // EmployerEmail
            //
            ValidationRules.AddRule(CommonRules.StringRequired, "Email");
            ValidationRules.AddRule(CommonRules.StringMaxLength, new CommonRules.MaxLengthRuleArgs("Email", 100));
            ValidationRules.AddRule <CSLA_Listing>(ValidateEmail, "Email");

 ...
 ...
}


My two custom validators are:

        //**********************************************************************
        /// <summary>
        /// Validate a optional email
        /// </summary>
        /// <param name="target"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private static bool ValidateEmail(CSLA_Listing target, Csla.Validation.RuleArgs e)
        {
            if (target._employerEmail.Length > 0)
            {
                Regex R = new Regex(@"[A-Za-z0-9\-.]+@[A-Za-z0-9\-.]+\.[A-Za-z][A-Za-z][A-Za-z]?[A-Za-z]?",RegexOptions.IgnoreCase);
                if (R.IsMatch(target._employerEmail) == false)
                {
                    e.Description ="Invalid email address";
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// Validate a optional URL
        /// </summary>
        /// <param name="target"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private static bool ValidateURL(CSLA_Listing target, Csla.Validation.RuleArgs e)
        {
            if (target._employerWebsite.Length > 0)
            {
                Regex R = new Regex(@"^http\://[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(/\S*)?$", RegexOptions.IgnoreCase);
                if (R.IsMatch(target._employerWebsite) == false)
                {
                    e.Description = "Invalid website URL";
                    return false;
                }
            }
            return true;
        }


protected override void AddBusinessRules()
{
    AddCommonRules();
    AddCustomRules();
}


I have no null value or error values in the database...
   
Is this not a wierd list name ? "List`1 list"?   (in the exception results)


Any ideas?

//Andy

ajj3085 replied on Wednesday, October 25, 2006

Well somewhere something is using a null reference.

The List`1 means that List is a generic type, with one generic parameter.  In other words, it means List<T>. 

Likewise, Dictionary`2 would indicate Dictionary<T1, T2>.

You'll need the debugger to help you find where the null reference is.

HTH
Andy

david.wendelken replied on Wednesday, October 25, 2006

ajj3085:
Well somewhere something is using a null reference.

The List`1 means that List is a generic type, with one generic parameter.  In other words, it means List<T>. 

Likewise, Dictionary`2 would indicate Dictionary<T1, T2>.

You'll need the debugger to help you find where the null reference is.

HTH
Andy

Bless you for making that clear!  That might have taken days to figure out!

mikeclimbs replied on Wednesday, October 25, 2006

Dont know if its related.

I had a similar issue that was caused by using TransactionScope to handle transactions.  I kept getting an error in validation on insert. 

 I made a change I'm not sure exactly what it was and I starting getting errors about transaction scope not being registered.

I then changed my Bo's to use ADO for transactions and the error went away.

My BO's were generated using the CSLA templates so it was easy to recreate them.

 

Mike

Copyright (c) Marimer LLC