StrataFrame Forum

Basic FK data populate on Maintenance Forms

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

By Alex M. Lana - 10/30/2007

Greetings,

Maybe this is a simple and basic question, but as we're new to SF, here it goes.



ProdutcTable (ProductBO)

- ProdCod (PK)

- VendorCod (FK) --> VendorTable (VendorBO)



Have a Maintenance Form with ProductBO(form bind) and VendorBO,

have also a Dialog to browse Vendors binded to VendorBO.



My question is how can I fill the VendorBO "on Navigate" so the info binded to it will follow it's parent FK.

The form contains no grids. Just binded fields.

I need the form to understand the relation and fetch the FKs data on ProductBO Navigate.



Thanks,



Alex


By Ivan George Borges - 10/30/2007

Hi Alex.

Have you had a look at the help file, under "Defining a Relationship Between Business Objects" ?

Also, to have a practical example, open the SampleCRMApplication, probably under C:\Program Files\MicroFour\StrataFrame\VB.NET Samples\CRMApplication, and open the CustomerMaintenance.vb, check the Customers and CustomerCreditCards BOs. Also, individualy open the CustomerBO and check its ParentRelationship property.

Let me know if you need something else.

Abraços.

 

By Ivan George Borges - 10/30/2007

Also, individualy open the CustomerBO and check its ParentRelationship property.

Sorry, I meant the CustomerCreditCardsBO...

By Alex M. Lana - 10/31/2007

Hello Ivan,

The BOs are already configured like CRM sample app.

Our question is not how this relation works, but how to fetch data on Navigate, how to trigger the fetch of data on Navigate, as the original post.



My question is how can I fill the VendorBO "on Navigate"so the info binded to it will follow it's parent FK.

The form contains no grids. Just binded fields.

I need the form to understand the relation and fetch the FKs data on ProductBO Navigate.




And also I have lots of products without vendors, and in this case it will produce an error, because the framework tries to set an wrong equal statement (where int = string). This happens when the FK on the parent table is blank.

So our needs are:



01) Fetch child BO data from database only on parent BO navigation or

02) Avoid wrong where statements when child FK information is not present on parent BO.



Thanks,



Alex
By Greg McGuffey - 10/31/2007

Alex,



I think what you want is to handle the parent BO's navigated event. In that event, test if the FK is present and either load the child or clear it. E.g. If you have a ProductBO and an VendorBO, your Navigated event handler might look something like this:



Private Sub ProductBO_Navigated(e As NavigatedEventArgs)

'-- Test if the product has a vendor, assuming that the VendorID has

' has been setup to be zero if it's null in the db...you could also use a

' nullable type and the appropriate test with that data type.

If Me.ProductBO.VendorID > 0 Then

'-- Load the vendor associated with this product

Me.VendorBO.FillByPrimaryKey(Me.ProductBO.VendorID)

Else

'-- No vendor, so clear vendor Bo

Me.VendorBO.Clear()

End If

End Sub




Hope that helps!
By Peter Denton - 10/31/2007

G'day

It may be that I'm not understanding what you are trying to do, but I was looking for answers to similar problems a few months ago until the lightbulb came on. When you define the parent-child relationship (as indicated by Ivan) you then fill both the parent and child BOs with all the records from the related table/view when the form loads and SF takes care of it from there, you don't have to refresh the child BO. If there is too much data then you will have to do as Greg suggested.

Peter

By Ivan George Borges - 10/31/2007

Hi Alex.

Guess Greg has direct you already.

But, still, give a good look at the SampleCRMApplication's CustomerMaintenance code. Maybe you have already and I am being redundant here... if so, sorry! Blush

In there, you will find many interesting guys: Customers_EditingStateChanged, Customers_Navigated, FillByParentPrimaryKey, SeekToPrimaryKey...

Um ótimo dia! Wink

By Greg McGuffey - 10/31/2007

Peter Denton (10/31/2007)
G'day



It may be that I'm not understanding what you are trying to do, but I was looking for answers to similar problems a few months ago until the lightbulb came on. When you define the parent-child relationship (as indicated by Ivan)you then fill both the parent and child BOs with all the recordsfrom the related table/view when the form loads and SF takes care of it from there, you don't have to refresh the child BO. If there is too much data then you will have to do as Greg suggested.



Peter




Yeah, the method I suggested is definitely directed toward reducing the data pulled across the network. Most of my users are remote Crying
By Alex M. Lana - 11/1/2007

Hi Greg, thanks for your reply.

In the meantime I've already done the same, but my problem is to construct the navigated object in C#.

I'm doing like this:

// Fetch Child Data on Parent BO Navigation

private void sysProdutoBO1_Navigated(MicroFour.StrataFrame.Business.NavigatedEventArgs currNavig)

{

if (this.sysProdutoBO1.for_cod > 0)

{

this.sysFornecedorBO1.FillByPrimaryKey(this.sysProdutoBO1.for_cod);

}

}


The question I have now is how to construct it at Designer.cs

this.sysProdutoBO1.Navigated += new MicroFour.StrataFrame.Business.NavigatedEventArgs(this.sysProdutoBO1_Navigated);


I don't know which parameters to send, and if is this the right way to grab the event in C#.



Thanks everyone for the attention, regards,



Alex
By StrataFrame Team - 11/1/2007

Yep, that is the correct way to attach to an event in C#.  The AddHandler and RemoveHandler keywords are not available in C#, so the overload of the +/- operators is used to add/remove method calls to delegate variables (events).  So, to add the event:

obj.EventName += new EventHandlerType(EventHandlerMethodName);

and to remove the event handler, it's the - operator.  I honestly can't remember whether you have to save off the created delegate in a variable to remove it or if you can just use the line above and swap the + for a -... not sure.

Also note that C# does not have the Handles keyword or WithEvents keyword like VB does, so when you double-click a button in the designer and it creates the Click handler method for you, it is explicitly adding the line above for the event handler within the .designer.cs file for the form.

By Alex M. Lana - 11/1/2007

Thanks Ben, exactly this I'm trying to acomplish, but it seems that SF C# classes miss some methods present in VB.

I can't find anything to send as:

MicroFour.StrataFrame.Business.BusinessNavigatedPosition NavigatedPositionArg

and

IsCurrentDataTableRefilled

this.sysProdutoBO1.Navigated += new MicroFour.StrataFrame.Business.NavigatedEventArgs(

??? this.SysProdutoBO1.NavigatedPositionArg ???,

this.sysProdutoBO1.CurrentRowIndex,

??? this.sysProdutoBO1.IsCurrentDataTableRefilled ???);



namespace MicroFour.StrataFrame.Business

{

public class NavigatedEventArgs : EventArgs

{

// Summary:

// Initializes a new instance of the NavigatedEventArgs class.

//

// Parameters:

// NavigatedPositionArg:

//

// CurrentRowIndexArg:

public NavigatedEventArgs(BusinessNavigatedPosition NavigatedPositionArg, int CurrentRowIndexArg, bool IsCurrentDataTableRefilled);



// Summary:

// Gets the absolute index of the newly selected record within the business

// object.

public int CurrentRowIndex { get; }

//

// Summary:

// Gets a value that indicates whether this navigation was the result of the

// CurrentDataTable being refilled.

public bool IsCurrentDataTableRefilled { get; }

//

// Summary:

// Gets a value that indicates where the newly selected record is in relation

// to the entire visible recordset.

public BusinessNavigatedPosition NavigatedPosition { get; }

}

}





Hope you can help me, I think it may be pretty simple.

I only need a simple C# version of this VB code:

Private Sub sysProdutoBO1_Navigated(ByVal e As MicroFour.StrataFrame.Business.NavigatedEventArgs) Handles sysProdutoBO1.Navigated



End Sub




Thanks,



Alex
By StrataFrame Team - 11/5/2007

Aha, you don't want to add a new NavigatedEventArgs to the handler, you want to add a new method delegate to the handler... like this:

using MicroFour.StrataFrame.Business;

namespace SomeNamespace
{
   
    public class SomeClassHandlingTheNavigatedEvent
    {
         private void AddHandlerToBusinessObject(BusinessLayer bo)
         {
             bo.Navigated += new BusinessLayer.NavigatedEventHandler(this.MyNavigatedHandlingMethod);
         }

         private void MyNavigatedHandlingMethod(NavigatedEventArgs e)
         {
             //-- Do whatever you want with the e.[properties]
             //   as they will contain the indexes and such
         }
    }
}

The difference here is that you use the constructor of the event handler and pass the name of the method you want to execute.  Then, when the business object wants to fire it's Navigated event, it will call your method.

Also, if you've dropped a business object on a form, you can easily add a new event handler by going to the property sheet for the business object and clicking the events tab (the little lightning bolt at the top of the property sheet).  Scroll down to the Navigated event and double-click it.  The designer will add the handler and take you to the code file to the method that it created for you.

By Alex M. Lana - 11/6/2007

Smile

Thanks for the example Ben, it was exactly that.



Alex
By StrataFrame Team - 11/7/2007

Glad you got it working.