I get that exception in Dictionaries and Lists that have a custom class. Example:
List<DisplayAllQuestionsTable> dsa = (List<DisplayAllQuestionsTable>)Session["Display"];
The cast works for 10-20 times when i use the Session..and then it starts to throw the exception. If i live the pc on for about 20-30 mins..i can launch my web application as usual, and after 20 times of launching the code, it throws the same exception. Why does that happen?
Now i tested another more simple code with Sesson:
public partial class Default2 : System.Web.UI.Page
{
List<meem> moom = new List<meem>();
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < 20; i++)
{
meem m = new meem();
m.i = i;
moom.Add(m);
}
Session["meem"] = moom;
Button ew = new Button();
ew.Text = "Press me";
ew.Click += Click;
PlaceHolder1.Controls.Add(ew);
}
void Click(object sender, EventArgs e)
{
List<meem> moom = (List<meem>)Session["meem"];
foreach (var item in moom)
{
Label l = new Label();
l.Text = item.i.ToString();
this.Controls.Add(l);
}
}
}
class meem
{
public int i;
}
And it works 100%
The exception that I get:
Server Error in '/WebSite10' Application.
[A]System.Collections.Generic.List`1[DisplayAllQuestionsTable] cannot be cast to
[B]System.Collections.Generic.List`1[DisplayAllQuestionsTable].
Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither'
at location 'D:\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 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: [A]System.Collections.Generic.List`1[DisplayAllQuestionsTable] cannot be cast to [B]System.Collections.Generic.List`1[DisplayAllQuestionsTable]. Type A originates from 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the context 'LoadNeither' at location 'D:\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 'D:\WINDOWS\Microsoft.Net\assembly\GAC_32\mscorlib\v4.0_4.0.0.0__b77a5c561934e089\mscorlib.dll'.