Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random InvalidCastException

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'.
like image 962
Matrix001 Avatar asked Jan 23 '26 14:01

Matrix001


1 Answers

This code as is, List<DisplayAllQuestionsTable> dsa = (List<DisplayAllQuestionsTable>)Session["Display"];

will not cause a null reference exception only if you tried to use it.

Similarly this code List<test> l = (List<test>)Session["test"]; will not cause a null or invalid cast exception if Session["test"] is null. An invalid cast exception will only occur if Session["test"] is not null. It seems to me the object stored in Display has been deformed in someway.

like image 174
Syed Hussim Avatar answered Jan 26 '26 04:01

Syed Hussim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!