Exemplo n.º 1
0
 // Fyller cart-listview med produkterna som finns i cartProducts.
 private void ListProductsInCart()
 {
     ListViewCart.Items.Clear();
     ListViewCart.BeginUpdate();
     foreach (Product product in cartProducts)
     {
         string[] productValues = new string[3];
         productValues[0] = product.name;
         productValues[1] = product.price.ToString();
         productValues[2] = product.productType.ToString();
         ListViewCart.Items.Add(new ListViewItem(productValues));
     }
     ListViewCart.EndUpdate();
 }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable dt = new DataTable();
                DataRow   dr;
                dt.Columns.Add("ProductId");
                dt.Columns.Add("ProductName");
                dt.Columns.Add("Price");
                dt.Columns.Add("Link");
                dt.Columns.Add("UnitPriceNew");
                dt.Columns.Add("Quantity");
                dt.Columns.Add("TotalPrice");
                dt.Columns.Add("TotalPriceAll");

                if (Request.QueryString["id"] != null)
                {
                    if (Session["buyitems"] == null)
                    {
                        dr = dt.NewRow();
                        String        mycon   = constr;
                        SqlConnection scon    = new SqlConnection(mycon);
                        String        myquery = "select * from Product where ProductId=" + 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["ProductId"]    = ds.Tables[0].Rows[0]["ProductId"].ToString();
                        dr["ProductName"]  = ds.Tables[0].Rows[0]["ProductName"].ToString();
                        dr["Link"]         = ds.Tables[0].Rows[0]["Link"].ToString();
                        dr["UnitPriceNew"] = ds.Tables[0].Rows[0]["UnitPriceNew"].ToString();
                        dr["Quantity"]     = ds.Tables[0].Rows[0]["Quantity"].ToString();
                        //dr["TotalPriceAll"] = grandtotal().ToString();
                        //dr["Quantity"] = ds.Tables[0].Rows[0]["Quantity"].ToString();
                        //int quantity = Convert.ToInt16(Request.QueryString["Quantity"].ToString());
                        int unitpricenew = Convert.ToInt32(ds.Tables[0].Rows[0]["UnitPriceNew"].ToString());
                        int quantity     = Convert.ToInt16(ds.Tables[0].Rows[0]["Quantity"].ToString());
                        int Totalprice   = unitpricenew * quantity;
                        dr["TotalPrice"] = Totalprice;
                        dt.Rows.Add(dr);
                        ListViewCart.DataSource = dt;
                        ListViewCart.DataBind();
                        Session["buyitems"] = dt;
                        //ListViewCart.FooterRow.Cells[5].Text = "Total Amount";
                        //ListViewCart.FooterRow.Cells[6].Text = grandtotal().ToString();
                        //Response.Redirect("AddToCart.aspx");
                    }
                    else
                    {
                        dt = (DataTable)Session["buyitems"];
                        int sr;
                        sr = dt.Rows.Count;
                        dr = dt.NewRow();
                        String        mycon   = constr;
                        SqlConnection scon    = new SqlConnection(mycon);
                        String        myquery = "select * from Product where ProductId=" + 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["ProductId"]    = ds.Tables[0].Rows[0]["ProductId"].ToString();
                        dr["ProductName"]  = ds.Tables[0].Rows[0]["productname"].ToString();
                        dr["Link"]         = ds.Tables[0].Rows[0]["Link"].ToString();
                        dr["UnitPriceNew"] = ds.Tables[0].Rows[0]["UnitPriceNew"].ToString();
                        dr["Quantity"]     = ds.Tables[0].Rows[0]["Quantity"].ToString();
                        //dr["TotalPriceAll"] = grandtotal().ToString();
                        //dr["Quantity"] = ds.Tables[0].Rows[0]["Quantity"].ToString();
                        //int Quantity = Convert.ToInt16(Request.QueryString["Quantity"].ToString());
                        int unitpricenew = Convert.ToInt32(ds.Tables[0].Rows[0]["UnitPriceNew"].ToString());
                        int quantity     = Convert.ToInt16(ds.Tables[0].Rows[0]["Quantity"].ToString());
                        int Totalprice   = unitpricenew * quantity;
                        dr["TotalPrice"] = Totalprice;
                        dt.Rows.Add(dr);
                        ListViewCart.DataSource = dt;
                        ListViewCart.DataBind();
                        Session["buyitems"] = dt;
                    }
                }
                else
                {
                    dt = (DataTable)Session["buyitems"];
                    ListViewCart.DataSource = dt;
                    ListViewCart.DataBind();
                }
            }
        }