5

I'm a tad stuck trying to get a List to load from the ViewState using ASP.NET 4 and VB.NET. When I try to retrieve a collection using:

Public Property ItemsForImport As List(Of ImportItem)
    Get
        Return IIf(ViewState("ItemsForImport") Is Nothing, New List(Of ImportItem), CType(ViewState("ItemsForImport"), List(Of ImportItem)))
    End Get
    Set(value As List(Of ImportItem))
        ViewState("ItemsForImport") = value
    End Set
End Property

I get the exception:

[A]System.Collections.Generic.List`1[ImportItem] cannot be cast to [B]System.Collections.Generic.List`1[ImportItem]. 
Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'. 
Type B originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location     'C:\Windows\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.

Debugging shows that the collection is not null and contains 2 items. The class is only defined once and I've cleaned my temporary files from C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files

Generally I sometimes see this (I assume everyone does) when making rapid changes while debugging, but it goes after a couple of refreshes. Is there something I'm missing?

Echilon
  • 10,064
  • 33
  • 131
  • 217

1 Answers1

6

Check your project folders. You probably have a duplicate assembly referenced somewhere in the solution, which is causing a conflict. Do you have a parent project that references the same assembly?

Read through a few of these solutions to see if anything fits your problem:

InvalidCastException for two Objects of the same type

ASP.NET unloading assemblies in the bin

Error Rendering control - [A] cannot be cast to [B] in the context LoadNeither

Community
  • 1
  • 1
James Johnson
  • 45,496
  • 8
  • 73
  • 110
  • 1
    Interestingly, I'm using a class with a Serializable attribute. If I store it in the HttpContext's session instead of the page ViewState, the page seems to go away. – Echilon Aug 23 '11 at 11:17