This is the code which is removing the line when you click the delete button your plugin added:
- Code: Select all
JiwaFinancials.Jiwa.JiwaStockTake.StockTakeLine stockTakeLine = stockTakeFrom.StockTake.Manager.CollectionItemFactory.CreateCollectionItem<JiwaFinancials.Jiwa.JiwaStockTake.StockTakeLine>();
stockTakeLine.RecID = grdLines.get_GridText("Key", e.Row).ToString();
stockTakeLine.DeleteFlag = true;
stockTakeFrom.StockTake.StockTakeLines.Remove(stockTakeLine);
It's creating a new stock take line, setting the RecID to be the RecID corresponding to the line of the grid row, and then setting that *brand new* line's DeleteFlag to True, and then calling the Remove method of the StockTakeLines collection.
It appears to work, as the line is removed from the grid because the form receives an event that a line was removed, and it dutifully finds the line with the corresponding RecID in the grid and removes it.
But then you go to save, it's encountering now a line which was never added to be deleted. When you call Remove() on a JiwaCollection it takes the item passed and adds it to a DeletedCollection. That collection is what we look at to remove from the database on save. It's encountering an item which was not ever added to anything, and as such gets a null reference on some properties that are normally set when added - namely the StockTakeLines property which is a property fronting the Collection property.
You can fix this by replacing those 4 lines in your code with this:
- Code: Select all
JiwaFinancials.Jiwa.JiwaStockTake.StockTakeLine stockTakeLine = stockTakeFrom.StockTake.StockTakeLines[grdLines.get_GridText("Key", e.Row).ToString()];
stockTakeFrom.StockTake.StockTakeLines.Remove(stockTakeLine);
It's getting the item you are wanting to delete from the StockTakeLines JiwaCollection, and passing it to the Remove method. This is the way we delete from JiwaCollections everywhere - always have. This will then mean that on save the right items are in the DeletedCollection with the right properties set, so no error occurs on Save().
You can always engage us to write plugins for you instead of you fumbling about hallucinating random solutions. We do charge money, but our plugins work.