Save object to Session. Unable to cast MyClass to MyClass.

  • Thread starter Thread starter jamesb457
  • Start date Start date
J

jamesb457

I'm getting this error a lot, it was working reasonably well, but then
for no apparant reason, it stopped.

I have a class, basically:

public class ShoppingBasket
{
int count;
String[] Items;
public AddItem()
}

Im trying to save this objectto a session so i can use it after
postback.

protected Add_To_Basket(data)
{
mycart.AddItem();
Session["shopbasket"] = mycart;
}

and then restore it:

protected void Page_Load(object sender, EventArgs e)
{
ShoppingBasket shopcart = (ShoppingBasket)Session["shopcart"]; //
this line here error's.

}

But i keep getting the error
Unable to cast object of type 'ShoppingBasket' to type
'ShoppingBasket'

Can anyone help?
 
You've probably added a page called ShoppingBasket.aspx, which has added a
second ShoppingBasket type that your code is trying to cast to. Try being
more explicit and fully qualify the type;

YourNameSpace.ShoppingBasket shopcart =
(YourNameSpace.ShoppingBasket)Session["shopcart"];
 
I'll try playing about with it some more with that in mind and see
what happens.
All the code is in 1 file though(well 1 default.aspx file and one
default.aspx.cs codebehind file.)
Namespaces are all default.

You've probably added a page called ShoppingBasket.aspx, which has added a
second ShoppingBasket type that your code is trying to cast to. Try being
more explicit and fully qualify the type;

YourNameSpace.ShoppingBasket shopcart =
(YourNameSpace.ShoppingBasket)Session["shopcart"];


I'm getting this error a lot, it was working reasonably well, but then
for no apparant reason, it stopped.
I have a class, basically:
public class ShoppingBasket
{
int count;
String[] Items;
public AddItem()
}
Im trying to save this objectto a session so i can use it after
postback.
protected Add_To_Basket(data)
{
mycart.AddItem();
Session["shopbasket"] = mycart;
}
and then restore it:
protected void Page_Load(object sender, EventArgs e)
{
ShoppingBasket shopcart = (ShoppingBasket)Session["shopcart"]; //
this line here error's.

But i keep getting the error
Unable to cast object of type 'ShoppingBasket' to type
'ShoppingBasket'
Can anyone help?
 
Hi James,

if you have pasted the correct code, you're using different identifiers
for the session variable ("shopbasket" vs. "shopcart").

Hope this helps,

Cheers,

Roland

protected Add_To_Basket(data)
{
mycart.AddItem();
Session["shopbasket"] = mycart;
}
and then restore it:
protected void Page_Load(object sender, EventArgs e)
{
ShoppingBasket shopcart = (ShoppingBasket)Session["shopcart"]; //
this line here error's.
}
But i keep getting the error
Unable to cast object of type 'ShoppingBasket' to type
'ShoppingBasket'
Can anyone help?
 
Well spotted,
Thats not my real code, just a quick knock-up to give you an idea of
what im doing.

On doing some more probing I have discovered it works fine as long as
I remove all objects from the session prior to running the page.
If it still has values from the last session, it crashes.

For a temporary fix i've done this which seems to work:

Page_Load( args )
{
if (Page.IsPostBack == false)
Session.RemoveAll();
}

Don't know why though, the object is exactly the same as it was...
 
Back
Top