DataMapper.SetPropertyValue bug with Formview and using Nullable Types (DetailsView is ok)

DataMapper.SetPropertyValue bug with Formview and using Nullable Types (DetailsView is ok)

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


Blarm posted on Thursday, February 08, 2007

I am calling

Csla.Data.DataMapper.Map(e.Values, obj)

in XYZ_UpdateObject.

This works fine when I use a DetailsView to map an empty TextBox to a Nullable (of Int32), but not when I use a FormView. The problem occurs in SetPropertyValue because when using a DetailsView the value from the empty TextBox comes through as Nothing, whereas it comes through as  "" {String} from a FormView.

SetPropertyValue could easily check for this by changing

' types don't match, try to coerce types

If pType.Equals(GetType(Guid)) Then

propertyInfo.SetValue(target, New Guid(value.ToString), Nothing)

to

' types don't match, try to coerce types

If vType.Equals(GetType(String)) AndAlso String.IsNullOrEmpty(value.ToString) Then

propertyInfo.SetValue(target, Nothing, Nothing)

ElseIf pType.Equals(GetType(Guid)) Then

propertyInfo.SetValue(target, New Guid(value.ToString), Nothing)

Is this something that should be fixed.

RockfordLhotka replied on Thursday, February 08, 2007

I'm not sure that's the correct answer (need to consider any other side-effects it might cause, and whether there's a more direct/elegant solution by checking to see if pType is a Nullable<T>), but I've added it to the wish list.

Blarm replied on Friday, February 09, 2007

Rocky,

Yes that would be more elegant.

Also, on the lines at the beginning of the procedure:

      If value Is Nothing Then
        propertyInfo.SetValue(target, value, Nothing)
It might be worth doing the same check for pType is a Nullable<T> before try to set the target to nothing: Although that would also mean moving the line:

        Dim pType As Type = Utilities.GetPropertyType(propertyInfo.PropertyType)
up above this check.

 

Copyright (c) Marimer LLC