StrataFrame Forum

Changing the BO's editing state to new with values already filled.

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

By Vikram Saxena - 2/15/2007

I have added a custom button on the SF Maintenance form to copy the current row values of the business object. on the clicked event of the button(copy) what i am doing is

1-> Store the Current Row,

2-> Begin a new row for BO.

3-> fill the values by fetching from data row we have in 1-> .

4-> invoke edit on the BO.



here is the code.



DataRow currentRow = employeeBO1.CurrentRow;

employeeBO1.NewRow();

for (int i = 0; i < currentRow.ItemArray.Length; i++)

{

employeeBO1.CurrentRow[i] = currentRow[i];

}

employeeBO1.Edit();



Till this everything is working very fine but the problem occurs when i click save and there is any broken rules exists on the form. i got the message that There are 'X' broken rules present.

when i click on the form to correct the values ...Zing...the new record gone..i reached to the last record that i copied. This works fine if there are no broken rules present for the new record....Please help...its very urgent..
By Trent L. Taylor - 2/16/2007

Well, your code is kindof a dangerous copy Smile  You are not taking into account any primary key fields, etc.  If your business object is managed your PKs or your database is auto-incrementing, then this is not going to work because the second row is the same as your first...including the PK which should be handed out.

You may need to take your PK and FK fields into account when copying.

By Chan - 2/16/2007

Hi,

Can you provide any sample code to copy record?

Any plan to add copy feature to SF.NET? As this is quite a common function and been asked many times in forum.



Thank you
By Trent L. Taylor - 2/16/2007

It depends on the table row that is being copied.  For example:

Private Sub CopyRow(ByVal SourceBusinessObject As BusinessLayer, ByVal FieldsToSkip As System.Collections.Generic.List(Of String))
    '-- Establish Locals
    Dim loTemp As MyBO
    Dim loColumn As DataColumn

    '-- Copy the data into the temp BO
    loTemp.CopyDataFrom(SourceBusinessObject, StrataFrame.Business.BusinessCloneDataType.ClearAndFillFromDefaultView)

    '-- Set the index to the temp BO
    loTemp.SeekToPrimaryKey(SourceBusinessObject.MyPK)

    '-- Create a new row in the source BO
    SourceBusinessObject.NewRow()

    '-- Cycle through the columns
    For Each loColumn In SourceBusinessObject.CurrentDataTable.Columns
        '-- Check for skipped fields
        If FieldsToSkip.Contains(loColumn.Columnname) Then
            Continue For
        End If

        '-- Copy the data
        SourceBusinessObject.CurrentRow.Item(loColumn.ColumnName) = loTemp.CurrentRow.Item(loColumn.ColumnName)
    Next

    '-- Clean Up
    loTemp.Dispose()
   
End Sub

By StrataFrame Team - 2/16/2007

You can do a foreach loop through the columns of the CurrentDataTable and if the columns are included in the PrimaryKeyFields array, then don't copy them. 

As for the problem with the row disappearing... there is a property called AutoNavigateToFirstBrokenRow; try setting it to false and see if that fixes your problem.

By Greg McGuffey - 2/19/2007

1-> Store the Current Row,

2-> Begin a new row for BO.

3-> fill the values by fetching from data row we have in 1-> .

4-> invoke edit on the BO.




To avoid the whole PK issue, I'd just change the logic a smidge:



1. Store away the current row

2. Invoke Add on BO

3. Set all the data properties in the new row to those saved in step 1

4. Invoke Save on BO.

5. Invoke Edit on BO if you want user to edit the new copy.



This way however the BO is managing the PK will be honored and you're UI will automatically be navigated to the new copy...if I understand the problem correctly. Hope this helps Smile