Increase Quantity while adding same product to cart in asp.net



In this post How to Increase Quantity while adding same product to cart in asp.net

Quantity TextBox Alignment Centre Coding
------------------------------------------

style="text-align: center"

------------------------------------------

Important Coding for Buy Now Button
-------------------------------------

CommandArgument <%# Eval("designid")%>

CommandName  addtocart

--------------------------------------

PageLoad Coding
---------------

Session["addproduct"] = "false";

---------------

DataList Item Command Event Coding
-----------------------------------

         TextBox txtbox = (TextBox)(e.Item.FindControl("TextBox1"));
        Session["addproduct"] = "true";
        Response.Redirect("AddtoCart.aspx?id=" + e.CommandArgument.ToString()+"&Quantity="+txtbox.Text);

-------------------------------------

AddtoCart.aspx Page Coding
----------------------------

Important Namespaces
----------------------

using System.Data;
using System.Data.SqlClient;

----------------------
static variable

static Boolean availabledesignid = false;

----------------------

Page Load Coding
-----------------

 if (Session["addproduct"].ToString() == "true")
        {
            Session["addproduct"] = "false";
            DataTable dt = new DataTable();
            DataRow dr;
            dt.Columns.Add("sno");
            dt.Columns.Add("designid");
            dt.Columns.Add("productname");
            dt.Columns.Add("productimg");
            dt.Columns.Add("description");
            dt.Columns.Add("quantity");
            dt.Columns.Add("price");
            dt.Columns.Add("total");


            if (Request.QueryString["id"] != null)
            {
                if (Session["Buyitems"] == null)
                {

                    dr = dt.NewRow();
                    String mycon = "Data Source=sk-pc;Initial Catalog=shop;Integrated Security=True";
                    SqlConnection scon = new SqlConnection(mycon);
                    String myquery = "select * from productdetails where designid=" + Request.QueryString["id"];
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = myquery;
                    cmd.Connection = scon;
                    SqlDataAdapter da = new SqlDataAdapter();
                    da.SelectCommand = cmd;
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dr["sno"] = 1;
                    dr["designid"] = ds.Tables[0].Rows[0]["designid"].ToString();
                    dr["productname"] = ds.Tables[0].Rows[0]["productname"].ToString();
                    dr["productimg"] = ds.Tables[0].Rows[0]["productimage"].ToString();
                    dr["description"] = ds.Tables[0].Rows[0]["shortdescription"].ToString();
                    dr["quantity"] = Request.QueryString["quantity"];
                    dr["price"] = ds.Tables[0].Rows[0]["price"].ToString();
                    Int64 price = Convert.ToInt64(ds.Tables[0].Rows[0]["price"].ToString());
                    Int64 quantity = Convert.ToInt64(Request.QueryString["quantity"].ToString());
                    Int64 totalprice = price * quantity;
                    dr["total"] = totalprice;
                    dt.Rows.Add(dr);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                    Session["buyitems"] = dt;
                }
                else
                {
                    checkdesignid();
                    if(availabledesignid==true)
                    {
                        Response.Write("Designid " + Request.QueryString["id"] + " " + availabledesignid);
                        updatequantity();
                        DataTable dt1;
                        dt1 = (DataTable)Session["buyitems"];
                        GridView1.DataSource = dt1;
                        GridView1.DataBind();
                        availabledesignid = false;
                    }
                    else
                    {
                        dt = (DataTable)Session["buyitems"];
                        int sr;
                        sr = dt.Rows.Count;

                        dr = dt.NewRow();
                        String mycon = "Data Sourcesk-pc;Initial Catalog=shop;Integrated Security=True";
                        SqlConnection scon = new SqlConnection(mycon);
                        String myquery = "select * from productdetails where designid=" + Request.QueryString["id"];
                        SqlCommand cmd = new SqlCommand();
                        cmd.CommandText = myquery;
                        cmd.Connection = scon;
                        SqlDataAdapter da = new SqlDataAdapter();
                        da.SelectCommand = cmd;
                        DataSet ds = new DataSet();
                        da.Fill(ds);
                        dr["sno"] = sr + 1;
                        dr["designid"] = ds.Tables[0].Rows[0]["designid"].ToString();
                        dr["productname"] = ds.Tables[0].Rows[0]["productname"].ToString();
                        dr["productimg"] = ds.Tables[0].Rows[0]["productimage"].ToString();
                        dr["description"] = ds.Tables[0].Rows[0]["shortdescription"].ToString();
                        dr["quantity"] = Request.QueryString["quantity"];
                        dr["price"] = ds.Tables[0].Rows[0]["price"].ToString();
                        Int64 price = Convert.ToInt64(ds.Tables[0].Rows[0]["price"].ToString());
                        Int64 quantity = Convert.ToInt64(Request.QueryString["quantity"].ToString());
                        Int64 totalprice = price * quantity;
                        dr["total"] = totalprice;
                        dt.Rows.Add(dr);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                        Session["buyitems"] = dt;
                    }
                   

                }
            }


        }
        else
        {
            DataTable dt;
            dt = (DataTable)Session["buyitems"];
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

---------------------

UserDefined Methods.
-----------------------

private void checkdesignid()
    {
        DataTable dt1;
        int designid;
        int querydesignid = Convert.ToInt16(Request.QueryString["id"]);
        dt1 = (DataTable)Session["buyitems"];
        foreach (DataRow row in dt1.Rows)
        {
            designid=Convert.ToInt16(row["designid"].ToString());
            if(designid==querydesignid)
            {
                availabledesignid = true;
            }
        }
    }
    private void updatequantity()
    {
        DataTable dt1;
        int designid;
        int querydesignid = Convert.ToInt16(Request.QueryString["id"]);
        dt1 = (DataTable)Session["buyitems"];
        foreach (DataRow row in dt1.Rows)
        {
            designid = Convert.ToInt16(row["designid"].ToString());
            if (designid == querydesignid)
            {
                int newquantity= Convert.ToInt16(row["quantity"].ToString()) + Convert.ToInt16(Request.QueryString["quantity"].ToString());
                row["quantity"] = newquantity;
                Int64 price = Convert.ToInt64(row["price"].ToString());
                Int64 totalprice = price * newquantity;
                row["total"] = totalprice;
                break;
            }
        }
        Session["buyitems"] = dt1;
    }

0 Comment's

Comment Form

Submit Comment