Пример #1
0
        protected DataTable GetDataTable(List <ProductDTO> productList)
        {
            //DataTable initialization
            DataTable             dtItem     = new DataTable();
            IEnumerable <SizeDTO> enumerable = new List <SizeDTO>();

            //Colmuns declaration
            dtItem.Columns.Add("SizeID");
            dtItem.Columns.Add("ProductID");
            dtItem.Columns.Add("Name");
            dtItem.Columns.Add("Type");
            dtItem.Columns.Add("Size");
            dtItem.Columns.Add("Price");
            dtItem.Columns.Add("Stock");
            dtItem.Columns.Add("Status");

            foreach (ProductDTO p in productList)
            {
                enumerable = SBL.GetDetails(p.GetId());
                List <SizeDTO> asList = enumerable.ToList();

                for (int i = 0; i < asList.Count(); i++)
                {
                    DataRow dr = dtItem.NewRow();
                    dr["SizeID"]    = asList[i].GetID();
                    dr["ProductID"] = p.GetId();
                    dr["Name"]      = p.GetName();
                    dr["Type"]      = p.GetProductType();
                    dr["Size"]      = asList[i].GetSize();
                    dr["Price"]     = asList[i].GetPrice();
                    dr["Stock"]     = p.GetStock();
                    dr["Status"]    = p.GetStatus();

                    dtItem.Rows.Add(dr);
                }
            }
            return(dtItem);
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string idString = "";
            int    id;

            // get id from URL segment
            try
            {
                var segments = Request.GetFriendlyUrlSegments();
                idString = segments[0];
            }
            catch (Exception ex)
            {
                ex.GetBaseException();
                Debug.Write(ex.ToString());
                lblError.Text = "Error URL";
            }

            // get id from query string and try to parse
            if (!string.IsNullOrEmpty(idString) && int.TryParse(idString, out id))
            {
                //call product from Database
                ProductBL db = new ProductBL();
                SizeBL    sb = new SizeBL();

                if (IsPostBack)
                {
                    labelPrice.Text  = sb.GetPriceBySize(id, Convert.ToInt32(unitDropDownList.SelectedValue)).ToString();
                    totalAmount.Text = GetTotalAmount(labelPrice.Text, quantityDropDownList.SelectedValue.ToString());
                    lblResult.Text   = "";
                }
                if (!IsPostBack)
                {
                    // retrieve a prodcut from our db
                    var            product        = db.GetProduct(id);
                    var            details        = sb.GetDetails(product.GetId());
                    List <SizeDTO> productDetails = details.ToList();

                    //only display available products to the customer
                    if (product != null && product.GetStatus() == 1)
                    {
                        // set up detail page elements with data from the product
                        headerTitle.Text        = product.GetName();
                        headerSubtitle.Text     = product.GetShortInfo();
                        descriptionLabel.Text   = product.GetInfo();
                        destinationImg.ImageUrl = product.GetImgPath();
                        nameLabel.Text          = product.GetName();
                        labelProduct.Text       = product.GetProductType();
                        labelProducer.Text      = product.GetProducer();
                        for (int i = 0; i < productDetails.Count; i++)
                        {
                            unitDropDownList.Items.Add(productDetails[i].GetSize().ToString());
                        }
                        labelPrice.Text = sb.GetPriceBySize(id, Convert.ToInt32(unitDropDownList.SelectedValue)).ToString();
                        for (int i = 1; i < product.GetStock(); i++)
                        {
                            quantityDropDownList.Items.Add(i.ToString());
                        }
                        if (product.GetStock() <= 5)
                        {
                            lowStock.Text = $"Low stock. Only {product.GetStock()} available";
                        }
                        if (product.GetProductType() == "Cheese")
                        {
                            lblUnit.Text = "(gr)";
                        }
                        else
                        {
                            lblUnit.Text = "(ml)";
                        }
                    }
                    else
                    {
                        headerTitle.Text = "Product currently not available";
                    }
                }
            }
            else
            {
                lblError.Text = "Error URL";
            }
        }