Chan
|
|
Group: Forum Members
Posts: 533,
Visits: 2K
|
Hi,
I have two tables with 1:1 relationship, one BO for each table. I want to "sync" parent BO event to child BO. For example, calling ParentBO.Add() will automatically call ChildBO.Add, ParentBO.Edit() also call ChildBO.Edit and etc.
How to do this?
Thank you
|
|
|
StrataFrame Team
|
|
Group: StrataFrame Developers
Posts: 3K,
Visits: 2.5K
|
Within the child business object, you can attach a handler to the ParentBusinessObject's AfterAddNew event and add a new record to the child business object when it fires. You can also add a handler to the ParentBusinessObject's EditingStateChanged event and when the editing state is Editing, call Edit() on the child business object.
|
|
|
Chan
|
|
Group: Forum Members
Posts: 533,
Visits: 2K
|
Hi, I created an composite model class that host my parent and childs BO. I event bind them as suggested. But hit error as below.  If I set my child BO as form's PrimarkBusinessObject, it works fine. using System;
using System.Collections.Generic;
using System.Text;
using JK.Inventory.BusinessObject.Main;
using MicroFour.StrataFrame.Business;
using MicroFour.StrataFrame.Data;
namespace JK.Inventory.BusinessObject.Composite
{
public class CompaniesBOModel
{
#region "Private Fields"
private CompaniesBO _CompaniesBO;
private CompanyAddressBO _CompanyAddressBO;
private CompanyContactsBO _CompanyContactsBO;
#endregion "Private Fields"
#region "Constructor"
public CompaniesBOModel() :base()
{
}
public CompaniesBOModel(CompaniesBO toCompanyBO, CompanyAddressBO toCompanyAddressBO,
CompanyContactsBO toCompanyContactBO)
{
this._CompaniesBO = toCompanyBO;
this._CompanyAddressBO = toCompanyAddressBO;
this._CompanyContactsBO = toCompanyContactBO;
this._CompanyAddressBO.ParentBusinessObject = this._CompaniesBO;
this._CompanyContactsBO.ParentBusinessObject = this._CompaniesBO;
this._CompaniesBO.ChildAutoFilterOption = AutoChildFilterOptions.MatchCurrentRow;
this.BindBOEventHandler();
}
#endregion "Constructor"
#region "Methods"
private void BindBOEventHandler()
{
this._CompaniesBO.AfterAddNew += new CompaniesBO.AfterAddNewEventHandler(this.AfterCompaniesBOAddNewEventHandler);
this._CompaniesBO.AfterDelete += new CompaniesBO.AfterDeleteEventHandler(this.AfterCompaniesBODeleteEventHandler);
this._CompaniesBO.AfterSave += new CompaniesBO.AfterSaveEventHandler(this.AfterCompaniesBOSaveEventHandler);
this._CompaniesBO.AfterUndo += new CompaniesBO.AfterUndoEventHandler(this.AfterCompaniesBOUndoEventHandler);
this._CompaniesBO.EditingStateChanged += new CompaniesBO.EditingStateChangedEventHandler(this.CompaniesBOEditingStateChangedEventHandler);
}
#endregion "Methods"
#region "Event Handler"
private void AfterCompaniesBOAddNewEventHandler(EventArgs e)
{
//this._CompanyAddressBO.Add();
this._CompanyContactsBO.Add();
}
private void AfterCompaniesBOSaveEventHandler(AfterSaveUndoEventArgs e)
{
this._CompanyAddressBO.Save();
this._CompanyContactsBO.Save();
}
private void AfterCompaniesBOUndoEventHandler(AfterSaveUndoEventArgs e)
{
this._CompanyAddressBO.Undo(BusinessUndoType.AllRows);
this._CompanyContactsBO.Undo(BusinessUndoType.AllRows);
}
private void AfterCompaniesBODeleteEventHandler(AfterDeleteEventArgs e)
{
}
private void CompaniesBOEditingStateChangedEventHandler(EditingStateChangedEventArgs e)
{
if (e.EditingState == BusinessEditingState.Editing)
{
this._CompanyAddressBO.Edit();
this._CompanyContactsBO.Edit();
}
}
#endregion "Event Handler"
#region "Properties"
public CompaniesBO CompaniesBO
{
get
{
return this._CompaniesBO;
}
}
public CompanyAddressBO CompanyAddressBO
{
get
{
return this._CompanyAddressBO;
}
}
public CompanyContactsBO CompanyContactsBO
{
get
{
return this._CompanyContactsBO;
}
}
#endregion "Properties"
}
}
System.IndexOutOfRangeException was unhandled by user code
Message="Index 0 is either negative or above rows count."
Source="System.Data"
StackTrace:
at System.Data.DataView.GetElement(Int32 index)
at System.Data.DataView.get_Item(Int32 recordIndex)
at MicroFour.StrataFrame.Business.BusinessLayer.get_CurrentRow()
at JK.Inventory.BusinessObject.Main.CompanyContactsBO.set_ContactTypeID(ContactType value) in F:\Projects\Inventory\InventoryBusinessObject\Main\CompanyContactsBO.Designer.cs:line 306
at JK.Inventory.BusinessObject.Main.CompanyContactsBO.CompanyContacts_SetDefaultValues() in F:\Projects\Inventory\InventoryBusinessObject\Main\CompanyContactsBO.cs:line 76
at MicroFour.StrataFrame.Business.BusinessLayer.raise_SetDefaultValues()
at MicroFour.StrataFrame.Business.BusinessLayer.OnSetDefaultValues()
at MicroFour.StrataFrame.Business.BusinessLayer.NewRow()
at MicroFour.StrataFrame.Business.BusinessLayer.Add(Boolean CheckSecurity)
at MicroFour.StrataFrame.Business.BusinessLayer.Add()
at JK.Inventory.BusinessObject.Composite.CompaniesBOModel.AfterCompaniesBOAddNewEventHandler(EventArgs e) in F:\Projects\Inventory\InventoryBusinessObject\Composite\CompanyBOModel.cs:line 62
at MicroFour.StrataFrame.Business.BusinessLayer.raise_AfterAddNew(EventArgs e)
at MicroFour.StrataFrame.Business.BusinessLayer.OnAfterAddNew(EventArgs e)
at MicroFour.StrataFrame.Business.BusinessLayer.NewRow()
at MicroFour.StrataFrame.Business.BusinessLayer.Add(Boolean CheckSecurity)
at MicroFour.StrataFrame.UI.Windows.Forms.BaseForm.Add(Boolean CheckSecurity)
at MicroFour.StrataFrame.UI.Windows.Forms.MaintenanceFormToolStrip.cmdNew_Click(Object sender, EventArgs e)
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
|
|
|
Trent Taylor
|
|
Group: StrataFrame Developers
Posts: 6.6K,
Visits: 7K
|
Well, the error is telling you that you either do no have any records in the CompanyContactsBO or the CurrentRowIndex has been positioned outside of the valid records. More than likely there are no records. You can see that when the ContactTypeID property is being set through the SetDefaultvalues that the record pointer is wrong at this point. If you put a break point in the SetDefaultvalues of CompanyCOntactsBO, is the CurrentRowIndex set property and is the Count of the BO greater than 1?
|
|
|
Chan
|
|
Group: Forum Members
Posts: 533,
Visits: 2K
|
Hi,
When I debug, currentRowIndex=0, and Count = 1.
loRow returned from NewRow() is an object by the way.
Also, I just call CompanyContactBO.Add() in CompaniesBO.AfterAddNewEventHandler(). That all I did. I didn't see any complicated code here.
Do you see any problem from my code posted ? Or, do you think you need my project?
Thank you
|
|
|
Chan
|
|
Group: Forum Members
Posts: 533,
Visits: 2K
|
Hi,
FYI, during debugging, I found that _CurrentDataTable.Rows.Count = 1 but _CurrentDataTable.DefaultView.Count = 0
It is at call stack
MicroFour StrataFrame Business.dll!MicroFour.StrataFrame.Business.BusinessLayer.NewRow() Line 5599 + 0xa bytes Basic
|
|
|
Trent Taylor
|
|
Group: StrataFrame Developers
Posts: 6.6K,
Visits: 7K
|
If you look at the MyBO.Count property, is that set to 0? If so, then you have a filter applied to the BO which is "hiding" the new record when created and thus creating the error. Remove any filters before the Add() or NewRow() if the new record will not fall within the filter, otherwise you will have this problem.
|
|
|
Chan
|
|
Group: Forum Members
Posts: 533,
Visits: 2K
|
Hi,
How to set filter?
I don't even know I can do this.
I think it is better I post my project as attachment later. Hope you can take a look. May be I do some mistake someway
Sorry for any inconvinience
|
|
|
Trent Taylor
|
|
Group: StrataFrame Developers
Posts: 6.6K,
Visits: 7K
|
How to set filter?
This can be done a number of different ways. You can set the Filter property of a BO. Set the RowFilter property of the CurrentView or CurrentDataTable.DefaultView. Or you may have the ChildAutoFilterOption of the BO set to a value other than Off.
|
|
|
Chan
|
|
Group: Forum Members
Posts: 533,
Visits: 2K
|
Hi,
Yes, I have ChildAutoFilterOption set to True. But how to auto requery child BO opon parentBO requery if other than true?
|
|
|
Trent Taylor
|
|
Group: StrataFrame Developers
Posts: 6.6K,
Visits: 7K
|
This follows the same rules as a FIlter. When you are creating the new record, this child record is hidden since the FK value has not yet been set. Set the filter to OFF when adding a new record then you can set it back on after the new record has been added. This is your problem.
|
|
|
Chan
|
|
Group: Forum Members
Posts: 533,
Visits: 2K
|
Hi,
Where should I place the code to set ChildAutoFilterOption = false, and where to restore it back?
I tried it at ParentBO.BeforeAddNew and AfterAddNew but doesn't work.
|
|
|
Chan
|
|
Group: Forum Members
Posts: 533,
Visits: 2K
|
BTW, I am using SF Maintenance form.
|
|
|
Trent Taylor
|
|
Group: StrataFrame Developers
Posts: 6.6K,
Visits: 7K
|
Try this....first, turn off the filter altogether and see if your problem goes away. If so, then try setting both the auto filter and the Filter property itself in the BeforeAddNew event of the BO to which the record is being added. I see no reason why this wouldn't work. MyChildBO.ChildAutoFilter = Off MyChildBO.Filter = ""
|
|
|