473,385 Members | 1,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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?

Mar 14 '07 #1
4 1644
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"];

<ja*******@gmail.comwrote in message
news:11**********************@p15g2000hsd.googlegr oups.com...
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?

Mar 14 '07 #2
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.

On 14 Mar, 10:59, "Aidy" <a...@noemail.xxxa.comwrote:
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"];

<jamesb...@gmail.comwrote in message

news:11**********************@p15g2000hsd.googlegr oups.com...
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?

Mar 14 '07 #3
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
JB schrieb:
>>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?

Mar 14 '07 #4
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...

On 14 Mar, 12:42, Roland Dick <bris...@web.dewrote:
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
Mar 14 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dotnetguy | last post by:
Hello, having a FieldInfo object I'd like to get a reference to the corresponding field object. I know the type of the field object (so I can do a cast if it is required) but I don't know if...
5
by: Wei-Chao Hsu | last post by:
Hi, I try to put an address of object into an int. Like this myclass obj; int address; address=&obj; "myclass" is an object of class or struct. It seems to illegal to C++. Is there any...
3
by: thomson | last post by:
Hi all, Can any one tell me what happens when an object is casted to an interface, somthing bit deeper, the case is i do have an interface Ifunction which has got one method display(), and i...
0
by: Aleksey Dmitriyev | last post by:
I am using Session property for my own serializable objects. The code snippet is below: // C# ASP.NET code behind private void btnRun_Click(object sender, System.EventArgs e) { MyClass myObj...
14
by: Schoo | last post by:
I have an asp.net app that uses session objects (ag. session("UserID")). The app works fine in development/debug mode. I released it to the test server (Windows 2000 server with other .NET...
1
by: klopter | last post by:
I will describe what I want using the vector class and then point out the limitations of using vector. vector <MyClass> MyContainer. MyContainer.push( new MyClass(...) ) MyContainer now...
3
by: Benton | last post by:
Hi there, Currently I have a rather simple class with a few members: string userName; string password; string centers; bool canModify; I have correspondant GET properties for them in the...
9
by: Stephan Steiner | last post by:
Hi I seem to have a bit of trouble understanding one bit of how generics work: In C#, every class automatically derives from object, and inherits a bunch of properties (i.e. ToString()). Thus,...
1
by: sana krishna | last post by:
Hai, Pls anyone help me........................... I am using ASP.NET(C#). I want to display the data in a gridview and make the gridview to become editable and insert and delete. But i can't...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.