StrataFrame Forum

One to one relationship

http://forum.strataframe.net/Topic5803.aspx

By Chan - 1/14/2007

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
By Greg McGuffey - 1/14/2007

I'm not entirely sure, but I think the first step is to do a relationship between the parent/child. Open the designer for the child BO and you can set its parent (ParentBO I think). You'll set the FK fields here.



Then on a form, you drop both BOs (parent first). Then open the properties window of the of the child BO and set its parent BO (I know this is a bit confusing, but setting at the class level sets the relationship, while this ties two instances together). In any case, the properties window will only let you set the correct one in each case.



I know this does things like automatically setting the value of the FK field in the child to the parent's PK and to auto navigate (if the parent instance is configured to do this one the form) the child BO when the parent BO is navigated. My guess is that it won't automatically handle adding or editing. However, there are a bunch of event the BO provides. Simply add handlers to the appropriate ones. The EditingStateChanged event is likely the best bet. In this one you can check the editing state and then enter the add or edit state in the child BO. You can then handle either the AfterSave or BeforeSave event to save the child, I'm not sure which one would work better.



BTW, I'm just a user like yourself, but I figured I might get you started in the right direction. When the SF devs answer, we'll all learn some more about SF! BigGrin



Good luck.
By StrataFrame Team - 1/15/2007

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.
By Chan - 1/19/2007

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. Crying



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)

By Trent L. Taylor - 1/20/2007

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?
By Chan - 1/20/2007

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
By Chan - 1/21/2007

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
By Trent L. Taylor - 1/21/2007

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.
By Chan - 1/21/2007

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
By Trent L. Taylor - 1/22/2007

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.

By Chan - 1/22/2007

Hi,

Yes, I have ChildAutoFilterOption set to True. But how to auto requery child BO opon parentBO requery if other than true?
By Trent L. Taylor - 1/22/2007

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.
By Chan - 1/23/2007

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.
By Chan - 1/23/2007

BTW, I am using SF Maintenance form.
By Trent L. Taylor - 1/23/2007

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 = ""