PDA

View Full Version : سوال: مشکل با نمایش Array List



lordofphp
جمعه 10 آذر 1391, 21:44 عصر
به نام خدا
سلام دوستان من دارم یه سبد خرید به aspx مینویسم به سی شارپ
تقریبا تموم شده اما این قسمتش با مشکل بر میخورم


if (Session["cart"] == null)
Label1.Text = "سبدخرید شما تهیست لطفا با مراجعه به صفحه اصلی فروشگاه نسبت به خرید اقدام نمائید";
Class1 class1 = new Class1();
ArrayList cart = new ArrayList();
if (Session["cart"] != null)
{
cart = (ArrayList)Session["cart"];
foreach (Class1 ins in cart)
{

ListBox1.Items.Add(ins.name);

}


اما وقتی سبدخرید پر میشه صفحه کارت که سبد خرید نشون میده خالیه
محتویات کلاس هم همین:

public class Class1
{


public string name;




//
// TODO: Add constructor logic here
//

}


اما با لود صفحه ی کارت که محتویاتش بالا گذاشتم اررور:
Server Error in '/WebSite1' Application.

Unable to cast object of type 'System.String' to type 'Class1'.

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: Unable to cast object of type 'System.String' to type 'Class1'.

r_s1389@yahoo.com
جمعه 10 آذر 1391, 21:52 عصر
شما تو کدتون یک آبجکت ساختید از Class1 درسته باید تو حلقه از اون استفاده کنید این رو ببین


if (Session["cart"] == null)
Label1.Text = "سبدخرید شما تهیست لطفا با مراجعه به صفحه اصلی فروشگاه نسبت به خرید اقدام نمائید";
Class1 c1 = new Class1();
ArrayList cart = new ArrayList();
if (Session["cart"] != null)
{
cart = (ArrayList)Session["cart"];
foreach (c1 ins in cart)
{

ListBox1.Items.Add(ins.name);

}