NotNullValidator not working, (is not a valid value for Int32.)

Topics: Validation Application Block
May 16, 2007 at 2:47 PM
Hello, I got the following situation.

I have an entity with attributes for validation.
#region Propiedades
public int IdPerfil
{
get { return _idPerfil; }
set { _idPerfil = value; }
}
StringLengthValidator(5, 10, Ruleset="RuleSetA", MessageTemplate="Perfil debe ser de 5 a 50 caracteres")
public string DSPerfil
{
get { return _dsPerfil; }
set { _dsPerfil = value; }
}

NotNullValidator(MessageTemplate = "Missing values", Ruleset = "RuleSetA")
public int IdPrioridad
{
get { return _idPrioridad; }
set { _idPrioridad = value; }
}
#endregion

I have two proxyproperty validators.

<table>
<tr>
<td style="width: 100px">
DsPerfil</td>
<td style="width: 100px">
<cc1:PropertyProxyValidator ID="PropertyProxyValidator1" runat="server" ControlToValidate="TextBox1"
PropertyName="DSPerfil" RulesetName="RuleSetA" SourceTypeName="Avansoft.Cifin2.BusinessObjects.Perfil" SpecificationSource="Attributes"></cc1:PropertyProxyValidator></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
NmPrioridad</td>
<td style="width: 100px">
<cc1:PropertyProxyValidator ID="PropertyProxyValidator2" runat="server" ControlToValidate="TextBox2"
PropertyName="IdPrioridad" RulesetName="RuleSetA" SourceTypeName="Avansoft.Cifin2.BusinessObjects.Perfil" SpecificationSource="Attributes"></cc1:PropertyProxyValidator></td>
<td style="width: 100px">
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="3" style="text-align: center">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Guardar" /></td>
</tr>
</table>

But the second one, is not firing, it throws the errror. (is not a valid value for Int32.)

Index was outside the bounds of the array.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace:


IndexOutOfRangeException: Index was outside the bounds of the array.
System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) +371

Exception: is not a valid value for Int32.
System.ComponentModel.BaseNumberConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value) +390
Microsoft.Practices.EnterpriseLibrary.Validation.Integration.ValidationIntegrationHelper.PerformValueConversion(Object& value, String& valueRetrievalFailureMessage) +522
Microsoft.Practices.EnterpriseLibrary.Validation.Integration.ValidationIntegrationHelper.GetValue(Object& value, String& valueRetrievalFailureMessage) +60
Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.PropertyProxyValidator.GetValue(Object& value, String& valueAccessFailureMessage) +62
Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.PropertyMappedValidatorValueAccess.GetValue(Object source, Object& value, String& valueAccessFailureMessage) +168
Microsoft.Practices.EnterpriseLibrary.Validation.Validators.ValueAccessValidator.DoValidate(Object objectToValidate, Object currentTarget, String key, ValidationResults validationResults) +76
Microsoft.Practices.EnterpriseLibrary.Validation.Validator.Validate(Object target) +58
Microsoft.Practices.EnterpriseLibrary.Validation.Integration.AspNet.PropertyProxyValidator.EvaluateIsValid() +76
System.Web.UI.WebControls.BaseValidator.Validate() +86
System.Web.UI.Page.Validate() +133
System.Web.UI.Page.Validate(String validationGroup) +105
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +81
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102



May 17, 2007 at 1:58 PM
Two things.

First, a NotNullValidator on a ValueType, like Int32, does not make much sense. A ValueType will never be null.

Second, in some cases you will probably need to use the ValueConvert Event of the PropertyProxyValidator to help it convert values. All values coming from a TextBox will be of type string but the validators on IdPrioridad are expecting values of type Int32. Sending string.Empty or an invalid char will cause an error because it cannot be converted to Int32 without your help.

I just did a Screencast on ValueConvert:

Enterprise Library Screencasts

Regards,

Dave

_____________________________

David Hayden
Microsoft MVP C#
May 17, 2007 at 2:40 PM
Thank you, I will watch the screencast and tell you I have problems!