Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi.
i am trying to do a shopping cart project where whenever u add some product to the cart i will store in a session array with 3 variables. its a spectacle shop so.3 things are to be added into array is pid,total no of amount and lens type.bt that's not the big issue.
I have done the part to add the product details into an array. and showing the array in a dynamically created array. here in the table whenever its showing the details its created a delete button. now i have stuck to delete the product details from session whenever user click a particular button to delete a product from cart.

webapplication1 --->> here i am adding the details in 3 textboxes. and in webapplication2 its showing the array along with the delete button..

and the array is created in the cart.cs

webapplication1.aspx
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication25.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
        <br />
        <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
        <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/WebForm2.aspx">View table</asp:HyperLink>
    </div>
    </form>
</body>
</html

>




webapplication1.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication_Cart;

namespace WebApplication25
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Cart_array c = new Cart_array();
            Cart obj = new Cart();
            obj.pid = TextBox1.Text;
            obj.amount = Convert.ToInt32(TextBox2.Text);
            obj.lens_type = TextBox3.Text;
            if (Session["cart"] == null)
            {
                c.add(obj);
                Session["cart"] = c;
            }
            if (Session["cart"] != null)
            {
                //WebApplication_Cart.Cart_array obj2 = new WebApplication_Cart.Cart_array();
                //obj2 = (WebApplication_Cart.Cart_array)Session["cart"];
                c = (WebApplication_Cart.Cart_array)Session["cart"];
                c.add(obj);
            }
        }
    }

}




webapplication2.aspx
XML
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication25.WebForm2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text=""></asp:Label>

        <br />
        <asp:Table ID="tblName" runat="server" BorderWidth="1px">
        </asp:Table>

        <br />

    </div>
    </form>
</body>
</html

>



webapplication2.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication_Cart;

namespace WebApplication25
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["cart"] != null)
{
WebApplication_Cart.Cart_array obj1 = new WebApplication_Cart.Cart_array();
obj1=(WebApplication_Cart.Cart_array) Session["cart"];
for (int i = 0; i < obj1.len; i++)
{
TableRow t = new TableRow();
TableCell c1 = new TableCell();
TableCell c2 = new TableCell();
TableCell c3 = new TableCell();
TableCell tcButton = new TableCell();
Button btn = new Button();
c1.Text = obj1.obj[i].pid.ToString();
c2.Text = obj1.obj[i].amount.ToString();
c3.Text = obj1.obj[i].lens_type.ToString();
btn.Text = "delete";
tcButton.Controls.Add(btn);
t.Cells.Add(c1);
t.Cells.Add(c2);
t.Cells.Add(c3);
t.Cells.Add(tcButton);

tblName.Rows.Add(t);
//Label1.Text = obj1.obj[i].pid.ToString();
Label1.Text += obj1.obj[i].pid.ToString() +obj1.obj[i].amount.ToString() + obj1.obj[i].lens_type.ToString();
}
}
}
}
}


cart.cs
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WebApplication_Cart
{
    public class Cart
    {
        public string pid,lens_type;
        public int amount;
        private int p;

        /*public Cart(int p)
        {
            // TODO: Complete member initialization
            this.p = p;
        }*/

    }
    public class Cart_array
    {
        public Cart[] obj;
        public int len;
        public Cart_array()
        {
            len = 0;
            obj = new Cart[10];
            for (int i = 0; i < 10; i++)
            {
                obj[i] = new Cart();
            }
        }
        public void add(Cart e)
        {
            obj[len].pid = e.pid;
            obj[len].amount = e.amount;
            obj[len].lens_type = e.lens_type;
            len++;

        }

    }
}







pls help me out..
thank u
Posted
Comments
ZurdoDev 29-Apr-13 11:39am    
Sorry, too much code for me to read through and try to make sense. But, to answer your post title, just do Session[variable] = null;
Thanks7872 30-Apr-13 0:22am    
try posting the smallest code snippet possible.No one would like to go through this lengthy code and find out the solution. Mention clearly at which line or a code block you need help and whats the issue?if you want to clear just the session then Session[xyz]= null is the solution.what else you need.be more specific.

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