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

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?
 
A

Aidy

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"];
 
J

JB

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?
 
R

Roland Dick

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?
 
J

JB

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top