4 Replies - 210 Views - Last Post: 26 December 2018 - 06:30 AM

#1 barakkadosh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-June 16

shopping cart delete product from list in session

Posted 25 December 2018 - 04:54 AM

hii
i have list product in session
and i want to delete one product from the list in my session
i put the session in gridview but i cant to delete on product from this gridview
how can i do it ?

this calss for the addtocart
public class AddToCart
{
    public static List<ProductOrder> GetProducts(System.Web.SessionState.HttpSessionState Session)
    {
        if (Session["addtocart"] != null)
        {
            return (List<ProductOrder>) Session["addtocart"];
        }
        return new List<ProductOrder>();
    }
    public static void addtocart(System.Web.SessionState.HttpSessionState Session, ProductOrder po)
    {
        if (Session["addtocart"] == null)
        {
            Session["addtocart"] = new List<ProductOrder>();
        }
        var list = (List<ProductOrder>)Session["addtocart"];
        list.Add(po);
        Session["addtocart"] = list;
    }
}


this calsss for the productorder

public class ProductOrder
{
    public int Id { get; set; }
    public int ProductID { get; set; }
    public double Meter { get; set; }
    public double Quantity { get; set; }
    public double WOM { get; set; }
    public double Total { get; set; }
    public string Commants { get; set; }
}



and this the code i put the session in gridview
public partial class Test3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            
            GridView1.DataSource = Session["addtocart"];
            GridView1.DataBind();
            
        }
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        DataTable table = Session["addToCart"] as DataTable;// get data from session
        if (table != null)
        {
            foreach (DataRow item in table.Rows) //loop through all the row
            {
                if ((int)item["id"] == (int)e.Keys[0]) // convert id from object to int 
                {
                    table.Rows.Remove(item); // delete the row
                    break;
                }
            }
            GridView1.DataSource = table; // rebind data
            GridView1.DataBind();
        }
    }
 
}



Is This A Good Question/Topic? 0
  • +

Replies To: shopping cart delete product from list in session

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6767
  • View blog
  • Posts: 23,071
  • Joined: 05-May 12

Re: shopping cart delete product from list in session

Posted 25 December 2018 - 07:10 AM

Moving to ASP.NET WebForms...
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6767
  • View blog
  • Posts: 23,071
  • Joined: 05-May 12

Re: shopping cart delete product from list in session

Posted 25 December 2018 - 07:17 AM

First, you have to decide if your products in the session object is supposed to be in a list like you have in your AddToCart class, or as a table as you seem to be doing in your RowDeleting event handler.
Was This Post Helpful? 0
  • +
  • -

#4 barakkadosh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 29-June 16

Re: shopping cart delete product from list in session

Posted 26 December 2018 - 12:40 AM

i am sory i new with c#
the table i load from the session this is the firs time i do it
can you help me how can i display the session and delete one product or some after i put them in the cart ?

This post has been edited by Skydiver: 26 December 2018 - 06:25 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6767
  • View blog
  • Posts: 23,071
  • Joined: 05-May 12

Re: shopping cart delete product from list in session

Posted 26 December 2018 - 06:30 AM

There is no need to quote the post above yours. Just use the big Reply button or the Fast Reply area.

If you are just learning C#, I recommend first learning the language and concepts surrounding it before learning ASP.NET.

Once you know C#, then I recommend skipping ASP.NET with WebForms and instead learning to do ASP.NET with MVC instead. There is a much clearer distinction of the separation of concerns in MVC as compared to using WebForms. WebForms was originally conceived by Microsoft as a way to try to ease the transition of a traditional Windows desktop programmer into web programming. Unfortunately, they actually made things worse.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1