Hi All,

I know this may seam odd but I'm getting an error navigating through a datagrid of an invalid cast. The trouble is it is of the same types!! I working on customising some Sage code and in the form there is a datagrid that is populated by a series of Sage.Accounting.Stock.TraceableSOPAllocationItems (a batched stock item allocated to an SOP).

I want to give the user some further information on each item so they can decide which batch and bin to use. I want to go through each item in the grid and then go find this extra information for it. the trouble is when I go to navigate through, My VB.Net code throws the error

InvalidCastException was unhandled by user code, Unable to cast object of type 'Sage.Accounting.Stock.TraceableSOPAllocationItem' to type 'Sage.Accounting.Stock.TraceableSOPAllocationItem'.

There shouldn't be a cast going on here they are of the same type???? Here is a snipet of my code:

dim AllocatedItem As Sage.Accounting.Stock.TraceableSOPAllocationItem
if itemGrid.Datasource isnot Nothing then
    for each AllocatedItem in itemGrid.Datasource
end if

The error happens when I start looping....

Any ideas would be much appreciated..

5 Years
Discussion Span
Last Post by G_Waddell

I am suspecting that the issue is the AllocatedItem.

Correct me if I am wrong, but there will only be object types in the data source right?

You can place the code in a TryCast() to attempt to cast it to the AllocatedItem object.



Yes you are right, the datasource will be made up of TraceableSOPAllocationItems
The TraceableSOPAllocationItems contain properties some of which the grid displays, just not the one I'm looking for but it is held in the TraceableSOPAllocatedItem object


Are the TracebleSOPAllocationItems being stored directly to the grid, or is the grid bound to a database?


The problem is it's not the generic datagrid but a special Sage one!

If I go into debug mode and do a watch on Grid.datasource I can see there are three items of type Sage.Accounting.TraceableSOPAllocationItem (Sorry, I wrote this down wrong last time,) if I try to loop through them I get the error I mentioned earlier.

I can also access the Items in the grid more directly as Listitems the problem there is I can only really get the data that is visible on the Grid:

For Each lItem As ListItem In ItemGrid.Items
    sBatchno = lItem.SubItems(1).Text
    sBinName = lItem.SubItems(2).Text

I tried to tap into the listitem datasource (AllocatedItem = lItem.Datasource) but the code went into some sort of infinite loop!??? and collapsed out. Although if I do a watch on lItem.Datasource I can see it being returned as Sage.Accounting.TraceableSOPAllocationItem type. I played around somemore with the watch and found I could get to the actual thing I wanted Sage.Accounting.StockItem if I did lItem.Datasource.StockItem but if I try:

dim oStockItem = Sage.Accounting.StockItem
For Each lItem As ListItem In ItemGrid.Items
    sBatchno = lItem.SubItems(1).Text
    sBinName = lItem.SubItems(2).Text
    oStockItem = lItem.Datasource.StockItem 

I get another invalid cast error... In the end, I had to use the following:

 For Each lItem As ListItem In ItemGrid.Items
    sBatchno = lItem.SubItems(1).Text
    sBinName = lItem.SubItems(2).Text
    dim oObject as new Object  = lItem.Datasource.StockItem 
    sStockID =  oObject.PrimaryKey.Value.ToString

I'm starting to wonder if there is a problem with the program dll's though because I'm see odd behaviour compared to the developer documentation....

Anyway thanks for your help!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.