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);
}
}
}
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.