Problems validating a Decimal value

Topics: Validation Application Block
Jan 22, 2009 at 10:56 AM
Hi there,

I'm trying to validate a Decimal field using validation blocks (using the configuration, not by code, in the web.config file).

I have this configuration in the web.config file:

- Validation Application Block
   -> MyObject
      ->Rule Set
       ->Price
         ->AndCompositeValidator
            ->TypeConversionValidator (I set the target type to System.Decimal)
            ->RangeValidator (between 1 and 9999, both inclusive)

I 've set the message template for both validators.

When I run the app, and submit the form, I receive a strange message from the application blocks:

Value to validate is not of the expected type: expected System.String but got System.Decimal instead.


¿Someone knows whats going on?

Thanks!

Jan 22, 2009 at 12:18 PM
Hi,

Would you mind posting your config and your sample code for testing.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com
Jan 22, 2009 at 3:51 PM

Hi,

You are getting that message because, Type conversion Validator is:

Type Conversion Validator
This validator checks that a string can be converted to a specific type. For example, the validator can check that "6.32" can be converted to a Double type or that "2007-02-09" can be converted to a DateTime type.

Taken from: http://msdn.microsoft.com/en-us/library/dd203109.aspx

Its expecting a string type.

Valiant Dudan
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 22, 2009 at 5:04 PM
Hi,

Then, how should I validate that a textbox contains a decimal when the user can delete the content of that textox? In that case the value whould be a String.Empty ("") . Isn't this also a string?

Thanks!

I'm trying to build a little site to reproduce the problem.
Jan 23, 2009 at 2:00 AM
The TypeConversionValidator can handle that since it can't convert an empty string to a decimal.


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com

Jan 28, 2009 at 3:19 PM
Many thanksHi...

I can't solve it!

Is there any complete example of using this control on the internet? The documentation do not goes much deep in my opinion...

Here is my "problematic" code:

ASPX code:

<

body>

 

 

 

<form id="form1" runat="server">

 

 

 

<div>

 

 

 

 

<table>

 

 

 

<tr>

 

 

 

<td class="style1">

 

 

 

<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label></td>

 

 

 

<td class="style6">

 

 

 

<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

 

 

 

 

<cc1:PropertyProxyValidator ID="PropertyProxyValidator1" runat="server"

 

 

 

ControlToValidate="txtName" PropertyName="Name" RulesetName="Rule Set"

 

 

 

SourceTypeName="Classes.Sample">*</cc1:PropertyProxyValidator> </td>

 

 

 

 

<td rowspan = "3" class="style2">

 

 

 

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

 

 

 

<asp:Button ID="btnStart" runat="server" Text="Start" /> </td></tr>

 

 

 

 

<tr>

 

 

 

<td class="style1">

 

 

 

<asp:Label ID="lblSurname" runat="server" Text="Surname"></asp:Label></td>

 

 

 

<td class="style6">

 

 

 

<asp:TextBox ID="txtSurname" runat="server"></asp:TextBox>

 

 

 

 

<cc1:PropertyProxyValidator ID="PropertyProxyValidator2" runat="server"

 

 

 

ControlToValidate="txtSurname" PropertyName="Surname" RulesetName="Rule Set"

 

 

 

SourceTypeName="Classes.Sample">*</cc1:PropertyProxyValidator></td></tr>

 

 

 

 

<tr>

 

 

 

<td class="style1">

 

 

 

<asp:Label ID="lblPrice" runat="server" Text="Price"></asp:Label></td>

 

 

 

<td class="style6">

 

 

 

<igtxt:WebNumericEdit ID="numPrice" runat="server"></igtxt:WebNumericEdit>

 

 

 

 

<cc1:PropertyProxyValidator ID="PropertyProxyValidator3" runat="server"

 

 

 

ControlToValidate="numPrice" PropertyName="Price" RulesetName="Rule Set"

 

 

 

SourceTypeName="Classes.Sample">*</cc1:PropertyProxyValidator></td></tr></table>

 

 

 

 

</div>

 

 

 

<asp:ValidationSummary ID="ValidationSummary1" runat="server" />

 

 

 

</form>

 

 

 

<p>

 

 

 

&nbsp;</p>

 

</

 

body>

 

 





Class to validate:

Public

Class Sample

 

 

 

Private _name As String

 

 

 

Public Property Name() As String

 

 

 

Get

 

 

 

Return _name

 

 

 

End Get

 

 

 

Set(ByVal value As String)

 

_name = value

 

 

End Set

 

 

 

End Property

 

 

 

Private _surname As String

 

 

 

Public Property Surname() As String

 

 

 

Get

 

 

 

Return _surname

 

 

 

End Get

 

 

 

Set(ByVal value As String)

 

_surname = value

 

 

End Set

 

 

 

End Property

 

 

 

Private _price As Decimal

 

 

 

Public Property Price() As Decimal

 

 

 

Get

 

 

 

Return _price

 

 

 

End Get

 

 

 

Set(ByVal value As Decimal)

 

_price = value

 

 

End Set

 

 

 

End Property

 

End

 

Class

 

 





Web.config:

<

validation>

 

<

 

type defaultRuleset="Rule Set" assemblyName="Classes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"

 

 

 

name="Classes.Sample">

 

<

 

ruleset name="Rule Set">

 

<

 

properties>

 

<

 

property name="Name">

 

<

 

validator lowerBound="1" lowerBoundType="Inclusive" upperBound="10"

 

 

 

upperBoundType="Inclusive" negated="false" messageTemplate="The length name must be between 1 and 10"

 

 

 

messageTemplateResourceName="" messageTemplateResourceType=""

 

 

 

tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

name="String Length Validator" />

 

</

 

property>

 

<

 

property name="Surname">

 

<

 

validator lowerBound="1" lowerBoundType="Inclusive" upperBound="20"

 

 

 

upperBoundType="Inclusive" negated="false" messageTemplate="The length Surname must be between 1 and 10"

 

 

 

messageTemplateResourceName="" messageTemplateResourceType=""

 

 

 

tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.StringLengthValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

name="String Length Validator" />

 

</

 

property>

 

<

 

property name="Price">

 

<

 

validator type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.AndCompositeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

name="And Composite Validator">

 

<

 

validator targetType="System.Decimal, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

 

 

 

negated="false" messageTemplate="Error Type" messageTemplateResourceName=""

 

 

 

messageTemplateResourceType="" tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.TypeConversionValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

name="Type Conversion Validator" />

 

<

 

validator lowerBound="0" lowerBoundType="Inclusive" upperBound="1000"

 

 

 

upperBoundType="Inclusive" negated="false" messageTemplate="The pice must be between 0 and 1000"

 

 

 

messageTemplateResourceName="" messageTemplateResourceType=""

 

 

 

tag="" type="Microsoft.Practices.EnterpriseLibrary.Validation.Validators.RangeValidator, Microsoft.Practices.EnterpriseLibrary.Validation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"

 

 

 

name="Range Validator" />

 

</

 

validator>

 

</

 

property>

 

</

 

properties>

 

</

 

ruleset>

 

</

 

type>

 

</

 

validation>


 

 

Many thanks!
Jan 29, 2009 at 1:30 AM

A TypeConversionValidator always expects a string.  If you want to use it, you have to change your Price property to type string.  If you want to keep it as of type Decimal, you can just remove the TypeConversionValidator.  If the user enters a string which cannot be converted to a decimal value, it will display "The supplied value could not be converted to the target property type".


Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.
entlib.support@avanade.com