updating the quantity if the item already exists...

Mar 17 2009 4:43 AM

Hi to all,

I am working on a shopping cart project. Now there is a situation that whenever adds the same product again and again, only quantity should be updated instead of adding a new everytime. I unable to do this. I tried like this.


1        protected void Page_Load(object sender, EventArgs e)
2        {
3            try
4            {
5                HyperLink HLink = (HyperLink)Master.FindControl("HyperLink3");
6                if (HLink != null)
7                {
8                    HLink.Enabled = false;
9                }
10  
11  
12               if (!Page.IsPostBack)
13               {
14                   if (Session["Cart"] == null)
15                   {
16                       Lblmsg.Visible = true;
17                       dt = new DataTable();
18                       dt.Columns.Add("REF", typeof(string));
19                       dt.Columns.Add("Description", typeof(string));
20                       dt.Columns.Add("QTY", typeof(int));
21                       dt.Columns.Add("Price", typeof(float));
22                       dt.Columns.Add("Cost", typeof(float));
23                       get_data();
24                       Session["Cart"] = dt;
25                   }
26                   else
27                   {
28                       Lblmsg.Visible = false;
29                       dt = (DataTable)Session["Cart"];
30                       get_data();
31                       update_for_same();
32                       Session["Cart"] = dt;
33                   }
34               }
35           }
36           catch(Exception ee)
37           {
38               Lblmsg.Text = ee.Message;
39           }
40       }
41  
42       public void get_data()
43       {
44           try
45           {
46               pro_id = Request.QueryString["pr_id"];
47               quanti = Convert.ToInt32(Request.QueryString["quant"]);
48  
49               if (pro_id != null)
50               {
51                   SqlConnection con = new SqlConnection("Server=.; Database=eclsc; Trusted_Connection=yes");
52                   SqlCommand cmd = new SqlCommand();
53                   cmd.Connection = con;
54                   cmd.CommandText = "select ICEMACHINES.product_id, ICEMACHINES.sh_desc, ICEMACHINES.price
                                                         from ICEMACHINES where ICEMACHINES.product_id LIKE @product_id union
                                                         select GLASSWARE.product_id, GLASSWARE.sh_desc, GLASSWARE.price
                                                         from GLASSWARE where GLASSWARE.product_id LIKE @product_id ";
55                   cmd.Parameters.Add("@product_id", SqlDbType.NVarChar, 50).Value = pro_id;
56                   cmd.Connection.Open();
57                   SqlDataReader rdr = cmd.ExecuteReader();
58                   ArrayList arRole1 = new ArrayList();
59  
60                   while (rdr.Read())
61                   {
62                       desc = (rdr["sh_desc"]).ToString();
63                       id = (rdr["product_id"]).ToString();
64                       unitprice = Convert.ToSingle((rdr["price"]));
65                       cost = unitprice * quanti;
66  
67                   }
68                   cmd.Connection.Close();
69  
70                   DataRow myrow = dt.NewRow();
71                   myrow["REF"] = id;
72                   myrow["Description"] = desc;
73                   myrow["QTY"] = quanti;
74                   myrow["Price"] = unitprice;
75                   myrow["Cost"] = cost;
76  
77                   dt.Rows.Add(myrow);
78                   dt.AcceptChanges();
79                   Session["data"] = dt;
80                   GridView1.DataSource = dt;
81                   GridView1.DataBind();
82                  
83  
84               }
85               else
86               {
87                   int a = 1;
88               }
89               total_items = total_items + quanti;
90               total_Price = total_Price + cost;
91               Session["items"] = total_items;
92               Session["value"] = total_Price;
93           }
94           catch(Exception ee)
95           {
96               Lblmsg.Text = ee.Message;
97           }
98           }
99  
100      public void update_for_same()
101      {
102          try
103          {
104              foreach (GridViewRow rowItem in GridView1.Rows)
105              {
106                  count = GridView1.Rows.Count;
107                  //id_last = GridView1.DataKeys[count - 1].Value.ToString();
108                  index = rowItem.RowIndex;
109                  //Label lblProduct_Id = (Label)rowItem.FindControl("lblProduct_Id");
110                  id3 = GridView1.DataKeys[index].Value.ToString();
111                  //id3 = lblProduct_Id.Text;
112 
113                  TextBox TxtQTY = (TextBox)rowItem.Cells[2].FindControl("TxtQTY");
114                  Txt2 = Convert.ToInt32(TxtQTY.Text);
115              }
116          }
117          catch(Exception ee)
118          {
119              Lblmsg.Text = ee.Message;
120          }
121      }

 

But I dont know how to do this. Please assist me.


Answers (1)