Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot bind to the property or column "Column Name" on the DataSource. Parameter name: dataMember

I have 2 DTO classes:

public class AddressDto
{
    public string Street { get; set; }
    public string City { get; set; }
    public string PostCode { get: set: }
}

public class CustomerDto
{
    public int Number{ get; set; }
    public string Name { get; set; }
    public AddressDto Address { get: set: }

    public CustomerDto() 
    {
        Address = new AddressDto();
    }
}

I have a form with a binding source in it that binds to CustomerDto. I also have a custom control with the address fields. This custom control has a binding source that binds to AddressDto The control's textboxes are correctly bound to the address properties.

The control exposes the following property:

[Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
[Browsable(false)]
public object Address
{
    get { return bindingSource.DataSource; }
    set { bindingSource.DataSource = value; }
}

On one machine I do not get any errors on CheckBinding(). However on another machine I get the above exception when I try to open the form in runtime. In designtime everything works fine.

The control has 3 TextBoxes and the designer adds the following bindings:

this.bindingSource.AllowNew = true;
this.bindingSource.DataSource = typeof(AddressDto);

this.txtStreet.DataBindings.Add(new Binding("Text", this.bindingSource, "Street", true));
this.txtCity.DataBindings.Add(new Binding("Text", this.bindingSource, "City", true));
this.txtPostCode.DataBindings.Add(new Binding("Text", this.bindingSource, "PostCode", true));

Any ideas where the problem can be?

like image 651
Ivan-Mark Debono Avatar asked Mar 08 '16 18:03

Ivan-Mark Debono


1 Answers

I changed the code to:

    [Bindable(BindableSupport.Yes, BindingDirection.TwoWay)]
    [Browsable(false)]
    public object Address
    {
        get { return bindingSource.DataSource; }
        set 
        {
            if (value != null && value != System.DBNull.Value)
                bindingSource.DataSource = value;
            else
                bindingSource.DataSource = typeof(AddressDto);
        }
    }

Value was System.DBNull. With the above change, the exception is not thrown anymore.

This solves the problem. However, why value is DBNull is still not clear because I'm using pure POCO classes as my datasources for my bindingsources.

like image 126
Ivan-Mark Debono Avatar answered Nov 17 '22 10:11

Ivan-Mark Debono