1

I have 3 forms in my application : frmTrucks, frmEditTruck and frmEditContent. frmTrucks shows my Trucks in a grid. I add a truck, or choose one of the trucks from the grid to edit in frmEditTruck

    public void Edit()
    {
        using (NestedUnitOfWork nuow = session.BeginNestedUnitOfWork())
        {
            Truck currentTruck = nuow.GetNestedObject(
        xpcTruck[gvTruck.GetDataSourceRowIndex(gvTruck.FocusedRowHandle)])
              as Truck;
            using (frmEditTruck form = new frmEditTruck(currentTruck))
            {
                if (form.ShowDialog() == DialogResult.OK)
                    nuow.CommitChanges();
            }
        }
    }

in frmEditTruck there are some text boxes for truck properties and two buttons. btnSave and btnAddContent. btnSave saves the changes (now.CommitChanges();). btnAddContent's click code is :

    Truck truck;
    Session session;

    public frmEditTruck(Truck truck)
    {
        InitializeComponent();
        this.truck = truck;
        this.session = truck.Session;
    }


    private void btnAddContent_Click(object sender, EventArgs e)
    {
        TContent content = new TContent(session);
        using (frmEditTContent form = new frmEditTContent(content, truck))
        {
            if (form.ShowDialog() == DialogResult.OK)
                truck.TContents.Add(content);
        }
    }

it shows frmEditContent. I can Add content to my truck. the problem is when I press AddContent and then cancel it. After that when I press the save button on my frmEditTruck it would add an empty row to my Content Table. I want to fix this problem. how can I fix it? I'm not sure my problem is clear enough for you. Please let me know

public class Truck : XPObject
{
    .
    .
    .

    [Association("Truck-TContents")]
    public XPCollection<TContent> TContents { get { return GetCollection<TContent>("TContents"); } }

}

public class TContent : XPObject
{
    .
    .
    .
    private Truck truck;
    [Association("Truck-TContents")]
    public Truck Truck
    {
        get
        {
            return truck;
        }
        set
        {
            SetPropertyValue("Truck", ref truck, value);
        }
    }
}
mtf
  • 329
  • 2
  • 13
  • When you want to cancel adding a new record, just not to call the NestedUnitOfWork.CommitChanges method. Is this approach acceptable according to your real scenario? – Uranus Jun 30 '15 at 08:17
  • no because I want to save Truck changes. but I don't want to get the record of Content saved if someone clicks on btnAddContent to open frmEditTContent form and close it without further action. @Uranus – mtf Jul 01 '15 at 08:25
  • @Uranus please see my solution – mtf Jul 01 '15 at 10:02

1 Answers1

2
private void btnAddContent_Click(object sender, EventArgs e)
{
    TContent content = new TContent(session);
    using (frmEditTContent form = new frmEditTContent(content, truck))
    {
        if (form.ShowDialog() == DialogResult.OK)
            truck.TContents.Add(content);
    }
}

I've changed the code to:

    private void btnAddContent_Add(object sender, EventArgs e)
    {
        TContent content = new TContent(session);
        using (frmEditTContent form = new frmEditTContent(content, truck))
        {
            if (form.ShowDialog() == DialogResult.OK)
            {
                truck.TContents.Add(content);
            }
            else
            {
                if (session.TrackingChanges)
                    session.RollbackTransaction();
            }
        }
    }

and it works properly.

mtf
  • 329
  • 2
  • 13