CopyDataFrom


Author
Message
Ian Hammond
Ian Hammond
StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)
Group: Forum Members
Posts: 57, Visits: 277
I wish to make a copy of some data by loading the original, changing the name of a queue that it belongs to and writing it back to the BO but preserving the original data. When I look at the individual rows, the data is changed, but nothing seems to be saved into the backend database. Have I misinterpreted the use of CopyDataFrom: Please see attached code snippet

Many thanks for any assistance.

this.localPrintQBO.FillWithQueueItems(Global.CurrentUserId, Global.CurrentTableId, this._currentPrintQ);

DataTable loTable = new DataTable();

loTable = this.localPrintQBO.CurrentDataTable.Clone();

foreach (DataRow qRow in this.localPrintQBO.CurrentDataTable.Rows)

{

loTable.ImportRow(qRow);

}

//Change the copy to the target print queue

foreach (DataRow qRow in loTable.Rows)

{

qRow["QName"] = this.cboTargetQ.Text.ToString();

}

this.localPrintQBO.CopyDataFrom(loTable, BusinessCloneDataType.AppendDataToTableFromCompleteTable);


Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
I'm not quite sure I follow what you are trying to do. Does the original data not have a PK? Or are you not loading the PK in the fill method? I'm asking because that could cause some issues when reloading the data and then attempting to save it.



Some other things to think about. You could use the CopyDataFrom both directions, you need to save the changes to the BO and I'd grab the queue name before I enter the loop:



//-- Load original print queue items

this.localPrintQBO.FillWithQueueItems(Global.CurrentUserId, Global.CurrentTableId, this._currentPrintQ);



//-- Use another BO rather than a datatable. I'm going to guess

//   that the BOs name is PrintQBO.

PrintQBO changedPrintQBO = new PrintQBO;

changedPrintQBO.CopyDataFrom(localPrintQBO, BusinessCloneDataType.ClearAndFillFromCompleteTable);



//-- Get the new queues name from UI.

string newQueue = this.cboTargetQ.Text.ToString();



//Change the copy to the target print queue

foreach (PrintQBO qRow in changedPrintQBO.GetEnumerable())

{

  qRow.QName = newQueue;

}



//-- This appends the data, but if the PK was copied, I'm not sure what

//   this will do. It would either add another set of rows to the

//   datatable, with duplicate PKs (bad) or update the original

//   data with the changed data (bad). Unless I'm missing something...

this.localPrintQBO.CopyDataFrom(changedPrintQBO, BusinessCloneDataType.AppendDataToTableFromCompleteTable);



//-- Save the data.

this.localPrintQBO.Save();




Let me know if that helped. BigGrin
Edhy Rijo
E
StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)StrataFrame VIP (4.6K reputation)
Group: StrataFrame Users
Posts: 2.4K, Visits: 23K
Hi Ian,

I believe the problem is that the BO.CopyDataFrom() method does no copy the BO.EditingState for each record, so if you need to save the data you need to loop each record and set the EditingState property to Add or Edit.

I am not at my office so I cannot give you exact code, but I am almost sure that happened to me once.

Edhy Rijo

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (3.3K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Setting the QName via the BO should dirty it as well.
Ian Hammond
Ian Hammond
StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)
Group: Forum Members
Posts: 57, Visits: 277
Thanks Guys for your input. I liked the different slant on the code and also I learned some other techniques when using Strataframe. Unfortunatelly the code gave the same results I was experiencing. I replaced the final Save with the changedPrintQBO and this did Save data in the database but overwrote the original data that I was trying to Copy. I think the issues are related to the primary key which is copied across from the source BO to the destination BO.

Again, many thanks

Charles R Hankey
Charles R Hankey
Advanced StrataFrame User (922 reputation)Advanced StrataFrame User (922 reputation)Advanced StrataFrame User (922 reputation)Advanced StrataFrame User (922 reputation)Advanced StrataFrame User (922 reputation)Advanced StrataFrame User (922 reputation)Advanced StrataFrame User (922 reputation)Advanced StrataFrame User (922 reputation)Advanced StrataFrame User (922 reputation)
Group: Forum Members
Posts: 524, Visits: 30K
If you are copying data, are you copying it into the same BO? I mean, if you start with 10 records in the BO, do you want to end up with 20 records with just a different Qnumber for the added 10?



If so, I would create a new instance of the BO, iterate the BO you want to copy from using Getenumerable as Greg showed, and use newrow() in the second BO, generating a new PK and copy the field values to the new BO record, substituting the Q number you want.



Dustin Taylor
Dustin Taylor
StrataFrame Team Member (652 reputation)
Group: StrataFrame Users
Posts: 364, Visits: 771
Yep, Charles' approach would definately be the cleanest for this application, IMO. We do similar things several places in our own apps. Something like this:

//-- Create a new instance to copy into
MyBo MyNewBo = new MyBo();
foreach ( MyBO bo in MyBoInstance.GetEnumerable( ) )
{
   //-- Create a new row
   MyNewBo.NewRow( );
   //-- Set the necessary values (the ones that should not be default, and should be different than the source BO)
   MyNewBo.mb_CustomField = //-- Whatever value is appropriate;
   //-- Copy the values
   foreach ( DataColumn col in MyNewBo.CurrentDataTable.Columns )
   {
      //-- Skip certain columns
      if((col.ColumnName.Equals("mb_pk",StringComparison.OrdinalIgnoreCase)) ||
         (col.ColumnName.Equals("mb_CustomField", StringComparison.OrdinalIgnoreCase)) ||
         (col.ColumnName.Equals("mb_CreatedAt",StringComparison.OrdinalIgnoreCase)) ||
         (col.ColumnName.Equals("mb_CreatedBy",StringComparison.OrdinalIgnoreCase)) ||
         (col.ColumnName.Equals("mb_Version",StringComparison.OrdinalIgnoreCase)))
      {
         continue;
      }
      //-- Try to update the value
      MyNewBo.CurrentRow[col.ColumnName] = bo.CurrentRow[col.ColumnName];
   }
}


 

Ian Hammond
Ian Hammond
StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)StrataFrame Novice (65 reputation)
Group: Forum Members
Posts: 57, Visits: 277
Many thanks, guys, that works a treat.

Regards

GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search