By Ian Hammond - 4/19/2010
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 snippetMany 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 queueforeach (DataRow qRow in loTable.Rows){ qRow[ "QName"] = this.cboTargetQ.Text.ToString();} this.localPrintQBO.CopyDataFrom(loTable, BusinessCloneDataType.AppendDataToTableFromCompleteTable);
|
By Greg McGuffey - 4/19/2010
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.
|
By Edhy Rijo - 4/19/2010
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.
|
By Greg McGuffey - 4/19/2010
Setting the QName via the BO should dirty it as well.
|
By Ian Hammond - 4/19/2010
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
|
By Charles R Hankey - 4/20/2010
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.
|
By Dustin Taylor - 4/20/2010
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]; } }
|
By Ian Hammond - 4/20/2010
Many thanks, guys, that works a treat.Regards
|
|