C# ASP - System.InvalidCastException in foreach loop
-
I keep getting this error when trying to read items from my shopping cart session. I've tracked it down to the foreach loop I am doing. Unable to cast object of type 'BasketItem' to type 'BasketItem'. Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'.
foreach (BasketItem theItem in items) { count++; total += theItem.ReturnTotal(); }
items is defined as this:ArrayList items = (ArrayList)Session["shoppingCart"];
Session["shoppingCart"] is defined as the ArrayList cartArray:ArrayList cartArray; Session["shoppingCart"] = cartArray;
The BasketItem class is a protected class with a string, an int, and 2 doubles that holds information about the product and price. I checked the session to make sure the data is in it correctly. It seems to be fine because I can get the data out of the session and bind it to a datagrid with no issues. For some reason it doesn't like me doing a foreach loop. This works fine:ArrayList items = (ArrayList)Session["shoppingCart"]; GridView2.DataSource = items; GridView2.DataBind();
I am lost on this one and hope someone can provide some insight. Thank you. -
I keep getting this error when trying to read items from my shopping cart session. I've tracked it down to the foreach loop I am doing. Unable to cast object of type 'BasketItem' to type 'BasketItem'. Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'.
foreach (BasketItem theItem in items) { count++; total += theItem.ReturnTotal(); }
items is defined as this:ArrayList items = (ArrayList)Session["shoppingCart"];
Session["shoppingCart"] is defined as the ArrayList cartArray:ArrayList cartArray; Session["shoppingCart"] = cartArray;
The BasketItem class is a protected class with a string, an int, and 2 doubles that holds information about the product and price. I checked the session to make sure the data is in it correctly. It seems to be fine because I can get the data out of the session and bind it to a datagrid with no issues. For some reason it doesn't like me doing a foreach loop. This works fine:ArrayList items = (ArrayList)Session["shoppingCart"]; GridView2.DataSource = items; GridView2.DataBind();
I am lost on this one and hope someone can provide some insight. Thank you.InvalidTypecast wrote:
Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'.
Represents the error that occurs when an explicit conversion (casting operation) fails because the source type cannot be converted to the destination type. InvalidCastException Class[^]
Regards, Satips.
-
I keep getting this error when trying to read items from my shopping cart session. I've tracked it down to the foreach loop I am doing. Unable to cast object of type 'BasketItem' to type 'BasketItem'. Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'.
foreach (BasketItem theItem in items) { count++; total += theItem.ReturnTotal(); }
items is defined as this:ArrayList items = (ArrayList)Session["shoppingCart"];
Session["shoppingCart"] is defined as the ArrayList cartArray:ArrayList cartArray; Session["shoppingCart"] = cartArray;
The BasketItem class is a protected class with a string, an int, and 2 doubles that holds information about the product and price. I checked the session to make sure the data is in it correctly. It seems to be fine because I can get the data out of the session and bind it to a datagrid with no issues. For some reason it doesn't like me doing a foreach loop. This works fine:ArrayList items = (ArrayList)Session["shoppingCart"]; GridView2.DataSource = items; GridView2.DataBind();
I am lost on this one and hope someone can provide some insight. Thank you.what does cartArray points to? try this: foreach (Object theItem in items) { count++; total += ((BasketItem)theItem).ReturnTotal(); }
Regards, Arun Kumar.A
-
InvalidTypecast wrote:
Exception Details: System.InvalidCastException: Unable to cast object of type 'BasketItem' to type 'BasketItem'.
Represents the error that occurs when an explicit conversion (casting operation) fails because the source type cannot be converted to the destination type. InvalidCastException Class[^]
Regards, Satips.
I understand the error, but I just can't figure out why it can't convert. It seems like it should.
-
what does cartArray points to? try this: foreach (Object theItem in items) { count++; total += ((BasketItem)theItem).ReturnTotal(); }
Regards, Arun Kumar.A
Thanks everyone. I figured it out. I believe what I was doing wrong was declaring the BasketItem class in Default.aspx.cs and Default2.aspx.cs (where I was reading the session passed from Default.aspx). I moved the class into the AppCode folder into its own .cs file, commented out the class definition from the other two cs files, and the problem went away.
-
Thanks everyone. I figured it out. I believe what I was doing wrong was declaring the BasketItem class in Default.aspx.cs and Default2.aspx.cs (where I was reading the session passed from Default.aspx). I moved the class into the AppCode folder into its own .cs file, commented out the class definition from the other two cs files, and the problem went away.
Still why your name "InvalidTypecast" is unaltered? ;););)
Regards, Arun Kumar.A
-
Still why your name "InvalidTypecast" is unaltered? ;););)
Regards, Arun Kumar.A
Still he he could not get any other Name it seems.
Regards, Satips.
-
Still he he could not get any other Name it seems.
Regards, Satips.
;););)
Regards, Arun Kumar.A