Binding and unbinding child objects

Binding and unbinding child objects

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


msrs_it posted on Wednesday, September 15, 2010

Hi,

I'm new to CSLA.NET and using CSLA.NET 3.8.4 for developing windows application.

I'have a Customer object as a root object. It consists of Addresses, Phones, Emails and a list of preferred communications as child objects.

I'm using a separate form to add or edit the Address in Addresses child object.

When I want to save or cancel the changes the object, it raises the error with the following message "Edit level mismatch in AcceptChanges"

The Code I've used to open the form as follows

private void uddbAddress_Click(object sender, EventArgs e)
        {
            byte addressType = 0;
            if (!string.IsNullOrEmpty(uddbAddress.Tag.ToString())) addressType = (byte)uddbAddress.Tag;
            int index = -1;
            if (null == _customer.Addresses.GetItem(addressType))
            {
                _customer.Addresses.AddAddress();
                index = _customer.Addresses.Count - 1;
                _customer.Addresses[index].AddressTypeId = addressType;
            }
            else
            {
                index = _customer.Addresses.IndexOf(_customer.Addresses.GetItem(addressType));
            }

            FormAddress formAddress = new FormAddress(_customer.Addresses[index]);
            if (DialogResult.OK == formAddress.ShowDialog(this))
            {
                utxteAddress.Text = _customer.Addresses[index].ToString();
            }
            else
            {
                if (_customer.Addresses[index].IsNew)
                    _customer.Addresses.CancelNew(index);
            }
            this.customerBindingSource.ResetBindings(true);
            formAddress.Dispose();
        }

FormAddress Code as follows:

    private CustomerAddress _customerAddress;

    public FormAddress(object address)
    {
        InitializeComponent();
        SetBinding(address);
        PopulateCombos();
    }
    private void SetBinding(CustomerAddress customerAddress)
    {
        if (objectAddress is CustomerAddress)
        {
            _customerAddress = customerAddress;
            AddressBindingSource.DataSource = _customerAddress;
        }          
    }
       
    private void OKultraButton_Click(object sender, EventArgs e)
    {
        if (!(_customerAddress.IsValid))
        {
            MessageBox.Show("The address is not valid.", "Invalid Address",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return;
        }

        // Disable events
        this.AddressBindingSource.RaiseListChangedEvents = false;
        try
        {
            // Unbind the UI
            UnbindBindingSource(AddressBindingSource, true, false);
        }
        catch(Exception exception)
        {
            MessageBox.Show(exception.Message, exception.Source);
        }
        this.DialogResult = DialogResult.OK;
    }

    private void CancelultraButton_Click(object sender, EventArgs e)
    {
        this.AddressBindingSource.RaiseListChangedEvents = false;
        UnbindBindingSource(AddressBindingSource, false, false);

        if (!(_customerAddress.IsNew))
            _customerAddress.CancelEdit();
    }

 

Requires some guidance for binding and unbinding related to child objects.

Adavance appreciation for your help.

MSRS.

msrs_it replied on Wednesday, September 15, 2010

It's Working . There is as error in ChildObject. After clearing that error, it works fine. :)

Copyright (c) Marimer LLC