Exemple #1
0
    protected void AddButton_Click(object sender, EventArgs e)
    {
        ItemController itemController = new ItemController();

        Item newItem = new Item();

        newItem.Name         = ItemNameTextBox.Text;
        newItem.PricePerItem = Convert.ToDouble(PriceTextBox.Text);
        newItem.Description  = DescriptionTextBox.Text;
        string uploadFolderPath = Server.MapPath(@"/product_images/" + newItem.Id + "-" + newItem.Name + "/");

        if (isAuthorized)
        {
            if (!Directory.Exists(uploadFolderPath))
            {
                Directory.CreateDirectory(uploadFolderPath);
            }

            string filePath;
            if (ProductImageFileUpload.HasFile)
            {
                string uploadFilePath = uploadFolderPath + ProductImageFileUpload.FileName;
                ProductImageFileUpload.SaveAs(uploadFilePath);

                filePath = @"~/product_images/" + newItem.Id + "-" + newItem.Name + "/" + ProductImageFileUpload.FileName;
            }
            else
            {
                filePath = @"~/product_images/no_image.png";
            }

            newItem.ProductImagePath = filePath;

            itemController.addItem(newItem);

            ErrorLabel.Text = "Item added!";

            ItemNameTextBox.Text    = "";
            PriceTextBox.Text       = "";
            DescriptionTextBox.Text = "";

            ProductImageFileUpload.Dispose();
        }
        else
        {
            ErrorLabel.Text = "You are not authorized to enter new items in the database!";
        }
    }
    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        Item editedItem = new Item();

        editedItem              = itemController.retrieveItem(Convert.ToInt32(ItemIDTextBox.Text));
        editedItem.Name         = ItemNameTextBox.Text;
        editedItem.PricePerItem = Convert.ToDouble(PriceTextBox.Text);
        editedItem.Quantity     = Convert.ToInt32(StockTextBox.Text);
        editedItem.SalePrice    = Convert.ToDouble(SalePriceTextBox.Text);
        editedItem.Description  = Convert.ToString(DescriptionTextBox.Text);

        if (isAuthorized)
        {
            if (ProductImageFileUpload.HasFile)
            {
                string uploadFolderPath = Server.MapPath(@"~/product_images/" + editedItem.Id + "-" + editedItem.Name + "/");

                if (!Directory.Exists(uploadFolderPath))
                {
                    Directory.CreateDirectory(uploadFolderPath);
                }

                string uploadFilePath = uploadFolderPath + ProductImageFileUpload.FileName;

                ProductImageFileUpload.SaveAs(uploadFilePath);

                string filePath = @"~/product_images/" + editedItem.Id + "-" + editedItem.Name + "/" + ProductImageFileUpload.FileName;

                editedItem.ProductImagePath = filePath;
            }

            itemController.editItem(editedItem);

            itemController.updateSalePrice(editedItem.Id, editedItem.SalePrice);

            RefreshTable();
            ClearFields();
        }
        else
        {
            ErrorLabel.Text = "You are not authorized to update the inventory database!";
        }
    }