DataObject.GetValue() returns "null" when string property is empty

Jul 13, 2011 at 3:02 PM

Hello,

while playing around with ProxyLists (using the ProxyDemo sample) I wrote a Validationrule class in order to use it with a ProxyList. The only way to access a column's value is casting the object passed in the Validate-Method into a DataObject and then retrieving the column's value by using DataObject.GetValue(). Doing so I encountered that that the DataObject.GetValue-Method returns null when the given column is empty (here: string.Empty). Is this behaviour intended or is it a bug?

Here ist my approach(/workaround?) to check if the LastName of a person is given:

namespace Client.Validation
{
    public class PersonValidation: ValidationRule
    {

        public override string Validate(object obj)
        {
            return string.Empty;
        }


        public override string  Validate(object obj, string columnName)
        {
            string result = string.Empty;
            
            DataObject dataObject = (DataObject)obj;
            
            if (string.IsNullOrEmpty(columnName) || columnName == "LastName")
            {
                // If the column's value is an empty string then 
                // GetValue() returns "null" ... so ...
                if ((dataObject.GetValue(columnName) == null) || 
                    (dataObject.GetValue(columnName).ToString().Trim() == ""))
                {
                    result = "Lastname is a mandatory field!";
                }
            }

            return result;
        }

    }
}

Best Regards,

M. Seeger

Coordinator
Jul 14, 2011 at 10:35 AM

Hi M.

I can't reproduce this behavior. My sample works as expected with your code. Can you show me the code where you generate the proxy and attach the validation rule?

Jörg

Jul 14, 2011 at 3:15 PM


Hi Jörg,

as I wrote, I'm currently playing with your ProxyDemo project. The Validation rule I wrote is added in Class "PersonService" like this:

public IDataObjectProxyList GetPersons()
{
    IPersonService service = this.GetService();
    List<Person> results = service.GetPersons();
    IDataObjectProxyList list = results.ToDataObjectListProxy("Id");
           
    // adding the validation rule:
    list.ValidationRules.Add(new PersonValidation2());
          
    list.BeginEdit();
    return list;
}

The validation rule I posted before works as expected, that's affirmative. Sorry that I didn't post this one - which throws the exception I described:

public class PersonValidation2 : ValidationRule
{

    public override string Validate(object obj)
    {
        return string.Empty;
    }


    public override string Validate(object obj, string columnName)
    {
        string result = string.Empty;

        DataObject dataObject = (DataObject)obj;

        if (string.IsNullOrEmpty(columnName) || columnName == "LastName")
        {
            // This will throw an exception when the column's value is
            // an empty string: 
            if (string.IsNullOrEmpty((dataObject.GetValue(columnName).ToString())))
            {
                return "First name is mandatory!";
            }
        }

        return result;
    }

}

Here I expected the GetValue-Method to work properly and return an empty string if the bound ui-object (grid-cell) is left empty instead of returning "null" and so leading to a crash.

Regards,

Michael.

Coordinator
Jul 15, 2011 at 6:19 AM

Hi Michael,

it's a DataGridView issue. If you delete the value in the grid, it is set to null by default. To prevent this, set the DefaultValue property of the ColumnStyle:

this.personDataGridView.Columns["LastName"].DefaultCellStyle.NullValue = " ";

Regards,

Jörg

Jul 16, 2011 at 3:35 PM

Hi Jörg,

thank you for the information (... I'm not that familiar with DataGridView/WinForms).

Best Regards,

Michael.