Shopping Cart using Session Arraylist and Dataset in ASP.Net Update, Delete

Shopping cart using Session arraylist and dataset in ASP.Net Update, Delete :

Aspx code

-- CSS

<style type="text/css"> 

        .tblTD {

 

            width:200px;

            font-size:15px;

            text-align:center;

            color:white;

        }

    </style>

<asp:repeater id="rptProductList" runat="server" onitemdatabound="rptProductList_ItemDataBound"

    onitemcommand="rptProductList_ItemCommand">

    <HeaderTemplate>

         <div style="border:1px solid #6f6e6e">

              <table>

                   <tr style="background:#6f6e6e;height:25px">

                      <td class="tblTD"></td>

                       <td class="tblTD">

                             <asp:Label ID="lbl" Text="ProductName"                

                                     runat="server"> </asp:Label>

                      </td>

                      <td class="tblTD">

                             <asp:Label ID="Label1" Text="Price" runat="server">

                                 </asp:Label>

                     </td>

                     <td class="tblTD">

                           <asp:Label ID="Label2" Text="Qty" runat="server">

                                     </asp:Label>

                     </td>

                     <td class="tblTD">

                     </td>

                     <td class="tblTD">

                    </td>

           </tr>

    </HeaderTemplate>

    <ItemTemplate>

        <div>

           <tr>

             <td class="tblTD">

                  <asp:Image ID="Image1" runat="server"   

                        ImageUrl='<%#"Handler1.ashx?id="+ Eval("ID")%>'

                        Height="125px" Width="125px" />

             </td>

           <td class="tblTD" style="color:#6f6e6e">

                   <asp:Label ID="lblName" runat="server" 

                     Text='<%#Eval("pro") %>'></asp:Label>

           </td>

          <td class="tblTD" style="color:#6f6e6e">

                    <asp:Label ID="lblPrice" Text='<%#Eval("sp")%>'               

                     runat="server"></asp:Label>

                        </td>

          <td class="tblTD" style="color:#6f6e6e">

                   <asp:Label ID="lblQuantity"  runat="server"></asp:Label>

           </td>

           <td class="tblTD" style="color:#6f6e6e">

                  <asp:TextBox ID="txtQuantity" Width="50px"  runat="server">

                   </asp:TextBox>

 

--LinkButton Update CommandName,Command Argument

 

              <asp:LinkButton ID="lnkUpdate" runat="server"

                  Text="Update" CommandArgument='<%#Eval("ID") %>'

                CommandName="UpdateColumn">

               </asp:LinkButton>

          </td>

           <td class="tblTD" style="color:#6f6e6e">

 

--LinkButton Delete CommandName,Command Argument

 

                <asp:LinkButton ID="lnkDelete" runat="server" Text="X"   

                     CommandArgument='<%#Eval("ID") %>'                

                     CommandName="DeleteColumn">  

                </asp:LinkButton>

                <asp:Label ID="lblID" runat="server"></asp:Label>

         </td>

      </tr>

            </ItemTemplate>

            <FooterTemplate>

                </table>

                </div>

            </FooterTemplate>

 </asp:repeater>

 
Aspx.CS
 

using System;

using System.Collections;

using System.Collections.Generic;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

 

 

namespace project

{

    public partial class Cart : System.Web.UI.Page

    {

        SqlConnection cn = new           

        SqlConnection(ConfigurationManager.AppSettings["Connection"]);

        SqlDataAdapter da = new SqlDataAdapter();

        System.Collections.ArrayList myArrayList = new ArrayList();

        DataSet ds = new DataSet();

        string aa;

        string DeleteID, UpdateID,UpdateValue;

        int bb=0;

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                ds = (DataSet)Session["CartDS"];

                View();

            }

        }

        public void View()

        {

            string[] arrID = new string[20];

            int Count;

            if (Session["CartDS"] != null)

            {

                if (ds.Tables[0].Rows.Count != 0)

                {

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                    {

                        arrID[i] = ds.Tables[0].Rows[i]["ID"].ToString() + ",";

                        aa += arrID[i];

                    }

                    aa = aa.Trim(',');

                    string Select;

                    Select = "Select * From tbl_product where ID in (" + aa + ")";

                    da = new SqlDataAdapter(Select, cn);

                    DataSet ds1 = new DataSet();

                    da.Fill(ds1);

                    rptProductList.DataSource = ds1;

                    rptProductList.DataBind();

 

    //Quantity Add

                   

             foreach (RepeaterItem rpt in rptProductList.Items)

             {

                     Label lbl = (Label)rpt.FindControl("lblQuantity");

                     Label lblID = (Label)rpt.FindControl("lblID");

                     TextBox txtQuantity =                 

                           (TextBox)rpt.FindControl("txtQuantity");

                     

                     lbl.Text = ds.Tables[0].Rows[rpt.ItemIndex] 

                                    ["items"].ToString();

 

 

                    lblID.Text = ds.Tables[0].Rows[rpt.ItemIndex]  

                                  ["ID"].ToString();

 

 

                    txtQuantity.Text = ds.Tables[0].Rows[rpt.ItemIndex 

                                    ["items"].ToString();

                }

                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                {

                    arrID[i] = ds.Tables[0].Rows[i]["items"].ToString();

                    bb += Convert.ToInt16(arrID[i]);

                }

                string cc = bb.ToString();

                string Cou = ds1.Tables[0].Rows.Count.ToString();

                Session["ItemCount"] = cc.ToString();

             }

             else

             {

                lblCartNotselected.Text = "You Selected Not Cart";

                Session["ItemCount"] = "0";

             }

        }

        else

        {

            Session["ItemCount"] = "0";

            lblCartNotselected.Text = "You Selected Not Cart ! !";

        }

    }

 

 //Repeater Control ItemCommand

 

        protected void rptProductList_ItemCommand(object source, RepeaterCommandEventArgs e)

        {            

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

            {

 

//Delete LinkButton CommandName and CommandArgument Passing

 

                if (e.CommandName == "DeleteColumn")

                {

                    Label lblQuantity = (Label)e.Item.FindControl("lblQuantity");

                    string aa = lblQuantity.Text;

                    

                    DeleteID = e.CommandArgument.ToString();

                    ds = (DataSet)Session["CartDS"];

                    DataTable dt1 = new DataTable();

                    dt1 = ds.Tables[0];

                    foreach (DataRow dr in ds.Tables[0].Rows)

                    {

                        if (dr["id"].ToString() == DeleteID)

                        {

         //Delete

                            dr.Delete();

                            ds.Tables[0].AcceptChanges();

                            break;

                        }

                       // ds.Tables[0].AcceptChanges();

                    }

                    View();

                    Response.Redirect("Cart.aspx");

                }

 

//Update LinkButton CommandName and CommandArgument Passing

 

 

                if (e.CommandName == "UpdateColumn")

                {

                    UpdateID = e.CommandArgument.ToString();

                    TextBox txtQuantity= 

                           (TextBox)e.Item.FindControl("txtQuantity");

                    ds = (DataSet)Session["CartDS"];

                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)

                    {

                        if (ds.Tables[0].Rows[i]["id"].ToString() == UpdateID)

                        {

                           //Update

                            ds.Tables[0].Rows[i]["items"] = txtQuantity.Text;

                                 ds.Tables[0].AcceptChanges();

                            break;

                        }

                    }

                    View();

                }

            }

        }

    }

}

Image1.jpg
 

Using Session and DataSet

http://vijubook.blogspot.in/2013/11/shopping-cart-using-session-arraylist.html