hi I've just started learning C# and I 'm working with excersice . There is something wrong with my add and remove button , but I don't know how to fix it
here is the begining of my code and I'm sure that it correct also I have Item and orederdItem class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
public partial class excersice-1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ArrayList ar = BuildItems();
ArrayList temp = new ArrayList();
foreach (Item it in ar)
{
ListItem li = new ListItem(it.Desc + "," + it.Price , it.ID.ToString());
ListBox2.Items.Add(li);
}
ListBox2.SelectedIndex = 0;
TextBox1.Text = "1";
}
}
ArrayList BuildItems(){
ArrayList temp = new ArrayList();
temp.Add(new Item (11, "Burger", 15));
temp.Add(new Item(12, "Cheese Burger", 20));
temp.Add(new Item(13, "Kebab", 24));
temp.Add(new Item(20, "Chiken Schawerma", 18));
temp.Add(new Item(22, "Beef Schawerma", 20));
temp.Add(new Item(35, "Grilled Chiken", 25));
temp.Add(new Item(34, "Grilled Fish", 22));
temp.Add(new Item(40, "Big Pizza", 18));
temp.Add(new Item(42, "Small Pizza", 14));
temp.Add(new Item(50, "Falafel", 10));
return temp;
}
void DisplayList(ArrayList temp)
{
Label1.Text = "<table>";
Label1.Text = Label1.Text + "<tr><td>" + "ID"
+ "</td><td>" + "Description" + "</td><td>" + "Price" + "</td><td>" + "Quantity" + "</td><td>" + "Amount" + "</td></tr>";
foreach (OrderedItem s in temp)
{
Label1.Text = Label1.Text + "<tr><td>" + s.ID+ "</td><td>" + s.Desc + "</td><td>" + s.Price + "</td><td>" + s.Qt +"</td><td>"+ s.Qt * s.Price + "</td></tr>";
}
Label1.Text = Label1.Text + "</table>";
}
protected void AddButton1_Click(object sender, EventArgs e)
{
string choice = ListBox2.SelectedItem.Text;
string [] p = choice.Split(',');
string des = p[0];
int price = int.Parse(p[1]);
int id = int.Parse(ListBox2.SelectedValue);
int qt = int.Parse(TextBox1.Text);
OrderedItem a = new OrderedItem(id, des, qt, price);
ArrayList savedItem = new ArrayList();
if (Session["items"] != null)
{
savedItem = (ArrayList)Session["items"];
}
savedItem.Add(a);
Session["items"] = savedItem;
DisplayList(savedItem);
}
now I'm working with my remove button
in the remove button I'm trying to retrive ordered arraylist from session and then loop through the arraylist and find the object with the same id as what the user input in the textbox and then display the updated arrayList and save it in the session object
protected void RemoveButton2_Click(object sender, EventArgs e)
{
ArrayList ar = new ArrayList();
int id1 = int.Parse(TextBox2.Text);
if (Session["items"] != null)
{
Label1.Text = (String)Session["items"];
foreach (OrderedItem s in Session)
{
if (s.ID == id1)
{
ar.Remove(s);
}
else
{
Label2.Text = "Id not found";
}
Session["items"] = ar;
DisplayList(ar);
}
}
}