Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void LinkButton2_Click(object sender, EventArgs e)
        {      
            LinkButton lnk = (LinkButton)sender;
            IList<cartitems> objCartItemsList = new List<cartitems>();
            if (Session["CartItems"] == null)
            {
                CartItems objCartItems = new CartItems();
                objCartItems.ItemId = Convert.ToInt32(lnk.CommandArgument.Split(';')[0]);
                objCartItems.Quantity = 1;
                objCartItems.ItemImage = Convert.ToString(lnk.CommandArgument.Split(';')[1]);
                objCartItems.ItemName = Convert.ToString(lnk.CommandArgument.Split(';')[2]);
                objCartItems.Cost = Convert.ToInt32(lnk.CommandArgument.Split(';')[3]);
                objCartItemsList.Add(objCartItems);
                Session["CartItems"] = objCartItemsList;
            }
            else
            {
                objCartItemsList = (IList<cartitems>)Session["CartItems"];
                int index = -1;
                for (int i = 0; i < objCartItemsList.Count; i++)
                {
                    if (objCartItemsList[i].ItemId == Convert.ToInt32(lnk.CommandArgument.Split(';')[0]))
                    {
                        objCartItemsList[i].Quantity++;
                        Session["CartItems"] = objCartItemsList;
                        index = i;
                        break;
                    }
                }
                if (index == -1)
                {
                    CartItems objCartItems = new CartItems();
                    objCartItems.ItemId = Convert.ToInt32(lnk.CommandArgument.Split(';')[0]);
                    objCartItems.Quantity = 1;
                    objCartItems.ItemImage = Convert.ToString(lnk.CommandArgument.Split(';')[1]);
                    objCartItems.ItemName = Convert.ToString(lnk.CommandArgument.Split(';')[2]);
                    objCartItems.Cost = Convert.ToInt32(lnk.CommandArgument.Split(';')[3]);
                    objCartItemsList.Add(objCartItems);
                    Session["CartItems"] = objCartItemsList;
                }
            }
            Response.Redirect("CartDetails.aspx");       
        }

aving Item details like ItemId, quantity , cost itemname in a session. If customer wants to buy same item qunatity of 2 then cost should multiply by quantity. In abouve code cost is not multiplying how to do it..
Posted
Updated 22-Aug-12 18:36pm
v3
Comments
Kenneth Haugland 23-Aug-12 0:21am    
Dont understand what your actrual problem is. Could you elaborate?
Lakshmimsridhar 23-Aug-12 0:31am    
saving Item details like ItemId, quantity , cost itemname in a session. If customer wants to buy same item qunatity of 2 then cost should multiply by quantity. In abouve code cost is not multiplying how to do it.. Hope now you understanding
Lakshmimsridhar 23-Aug-12 0:24am    
saving Item details like ItemId, quantity , cost itemname in a session. If customer wants to buy same item qunatity of 2 then cost should multiply by quantity. In abouve code cost is not multiplying how to do it

1 solution

C#
    LinkButton lnk = (LinkButton)sender;
    IList<cartitems> objCartItemsList = new List<cartitems>();
    if (Session["CartItems"] == null)
    {
        CartItems objCartItems = new CartItems();
        objCartItems.ItemId = Convert.ToInt32(lnk.CommandArgument.Split(';')[0]);
        objCartItems.Quantity = 1;
        objCartItems.ItemImage = Convert.ToString(lnk.CommandArgument.Split(';')[1]);
        objCartItems.ItemName = Convert.ToString(lnk.CommandArgument.Split(';')[2]);
        objCartItems.Cost = Convert.ToInt32(lnk.CommandArgument.Split(';')[3]);
        objCartItemsList.Add(objCartItems);
        Session["CartItems"] = objCartItemsList;
    }
    else
    {
        objCartItemsList = (IList<cartitems>)Session["CartItems"];
        int index = -1;
        for (int i = 0; i < objCartItemsList.Count; i++)
        {
            if (objCartItemsList[i].ItemId == Convert.ToInt32(lnk.CommandArgument.Split(';')[0]))
            {
                objCartItemsList[i].Quantity++;
                objCartItemsList[i].Cost = (Convert.ToInt32(lnk.CommandArgument.Split(';')[3]) * objCartItemsList[i].Quantity);
                Session["CartItems"] = objCartItemsList;
                index = i;
                break;
            }
        }
        if (index == -1)
        {
            CartItems objCartItems = new CartItems();
            objCartItems.ItemId = Convert.ToInt32(lnk.CommandArgument.Split(';')[0]);
            objCartItems.Quantity = 1;
            objCartItems.ItemImage = Convert.ToString(lnk.CommandArgument.Split(';')[1]);
            objCartItems.ItemName = Convert.ToString(lnk.CommandArgument.Split(';')[2]);
            objCartItems.Cost = Convert.ToInt32(lnk.CommandArgument.Split(';')[3]);
            objCartItemsList.Add(objCartItems);
            Session["CartItems"] = objCartItemsList;
        }
    }
    Response.Redirect("CartDetails.aspx");
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900