New ECOMtl Row in Eng Workbench

Hi Guys,
I’m bashing my head against the wall trying to get a new row into a new ECOMtl in engineering workbench via a customization.

When i try and set the MtlPartNum like the trace i get “Index -1 is either negative or above rows count.” exception.

I was using a BeginEdit and EndEdit() around the changes to the view but i am not sure when/where i should use Begin/End ( I thought it was every ds change).

I can read the MtlPartNum and modify other values e.g. UOM.

I’ve looked at the traces and everything looks correct, I have almost the same code in some external code and it works it’s just something wrong with the dataset?

Any help/pointers would be appreciated - i can spit out more of the exception if it would be helpful.

var ds = this.oTrans.Factory("ECOMtl");

var adapter = ((EngWorkBenchAdapter)oTrans_engWorkBenchAdapter);
adapter.GetNewECOMtl("GroupID", "PartNum", "Rev", "");
string outMsg = "";
string opMsgType = "";
adapter.CheckECOMtlMtlPartNum("MtlPartNm", out outMsg, out opMsgType);

//ds.dataView[0].BeginEdit();
ds.dataView[0]["MtlPartNum"] = "MtlPartNm";
//ds.dataView[0].EndEdit();

adapter.ChangeECOMtlMtlPartNum();

When you are doing it via the adapter the index 0 might not exist yet, so you need to assign it on the adapters data not the dataView (since its not there yet).

Not Tested, just quickly wrote up

var adapter = ((EngWorkBenchAdapter)oTrans_engWorkBenchAdapter);

// GetNewECOMtl merely populates the DataTable in the adapter with defaults
adapter.GetNewECOMtl("GroupID", "PartNum", "Rev", "");

// This is where you make changes
var table = adapter.EngWorkBenchData.ECOMtl;
int i = (table.Rows.Count - 1);
table.Rows[i]["MtlPartNum"] = "MyMtlPartNum";

// Not even sure if you need to call this yet on a new Record, but do as the trace does
adapter.ChangeECOMtlMtlPartNum();

// Update actually adds it (commits it) from being "temporary" to the database
adapter.Update();

// perhaps even call
var dvECOMtl = this.oTrans.Factory("ECOMtl");
dvECOMtl.Notify(new EpiNotifyArgs(oTrans, dvECOMtl.dataView.Count - 1, EpiTransaction.NotifyType.AddRow));

thanks for the quick reply!

I can access the row in the adapter after i call GetNewECOMtl.
I can read the MtlPartNum without an exception but i cannot set it.

I tried changing my code so i set the field via the adapter data table rather than the oTrans Factory (which i think is the epidataview?) but i still got an exception.

To be clear the exception is below which indicates it’s something that’s being triggered when i change the dataset.
Maybe it’s a datatable rule?

"   at System.Data.DataView.GetRow(Int32 index)
   at Erp.UI.App.EngWorkBenchEntry.Transaction.get_ecoMtlRow()
   at Erp.UI.App.EngWorkBenchEntry.Transaction.ECOMtl_ColumnChanging(Object sender, DataColumnChangeEventArgs e)
   at System.Data.DataTable.OnColumnChanging(DataColumnChangeEventArgs e)
   at Erp.BO.EngWorkBenchDataSet.ECOMtlDataTable.OnColumnChanging(DataColumnChangeEventArgs e)
   at System.Data.DataRow.set_Item(DataColumn column, Object value)
   at System.Data.DataRow.set_Item(String columnName, Object value)"

Paste your new code as-is.

interestingly i added a row via the ui to test there wasnt something wrong with the group and then it worked.
but if i go and delete all the rows and start from row 0 it won’t work again.
this is very strange as i’ve had a very good look at the traces and i think i’m setting everything correctly.

var ds = this.oTrans.Factory("ECOMtl");

        var adapter = ((EngWorkBenchAdapter)oTrans_engWorkBenchAdapter);

        adapter.GetNewECOMtl("xx", "xx", "xx", "");

        string outMsg = "";

        string opMsgType = "";

        adapter.CheckECOMtlMtlPartNum("2", out outMsg, out opMsgType);

        var table = adapter.EngWorkBenchData.ECOMtl;

        int i = (table.Rows.Count - 1);

        table.Rows[i]["MtlPartNum"] = "2";