Esempio n. 1
0
    public bool UpdateProduct(string productName, string quantityPerUnit, int productID)
    {
        Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID);
        if (products.Count == 0)
        {
            // no matching record found, return false
            return(false);
        }

        Northwind.ProductsRow product = products[0];

        product.ProductName = productName;
        if (quantityPerUnit == null)
        {
            product.SetQuantityPerUnitNull();
        }
        else
        {
            product.QuantityPerUnit = quantityPerUnit;
        }

        // Update the product record
        int rowsAffected = Adapter.Update(product);

        // Return true if precisely one row was updated, otherwise false
        return(rowsAffected == 1);
    }
Esempio n. 2
0
    protected void ItemDataBoundFormattingExample_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            // Programmatically reference the ProductsRow instance bound to this DataListItem
            Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Item.DataItem).Row;

            // See if the UnitPrice is not NULL and less than $20.00
            if (!product.IsUnitPriceNull() && product.UnitPrice < 20)
            {
                // Highlight the product name and unit price Labels
                // First, get a reference to the two Label Web controls
                Label ProductNameLabel = (Label)e.Item.FindControl("ProductNameLabel");
                Label UnitPriceLabel   = (Label)e.Item.FindControl("UnitPriceLabel");

                // Next, set their CssClass properties
                if (ProductNameLabel != null)
                {
                    ProductNameLabel.CssClass = "AffordablePriceEmphasis";
                }

                if (UnitPriceLabel != null)
                {
                    UnitPriceLabel.CssClass = "AffordablePriceEmphasis";
                }



                // Alternatively, you can opt to adjust the style for the *entire* item:
                // e.Item.CssClass = "AffordablePriceEmphasis";
            }
        }
    }
Esempio n. 3
0
    public bool UpdateProduct(string productName, decimal?unitPrice, int productID)
    {
        Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID);
        if (products.Count == 0)
        {
            // no matching record found, return false
            return(false);
        }

        Northwind.ProductsRow product = products[0];

        product.ProductName = productName;
        if (unitPrice == null)
        {
            product.SetUnitPriceNull();
        }
        else
        {
            product.UnitPrice = unitPrice.Value;
        }

        // Update the product record
        int rowsAffected = Adapter.Update(product);

        // Return true if precisely one row was updated, otherwise false
        return(rowsAffected == 1);
    }
Esempio n. 4
0
    protected void ProductsInCategory_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Reference the ProductsRow via the e.Row.DataItem property
            Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;

            // Increment the running totals (if they're not NULL!)
            if (!product.IsUnitPriceNull())
            {
                _totalUnitPrice += product.UnitPrice;
                _totalNonNullUnitPriceCount++;
            }

            if (!product.IsUnitsInStockNull())
            {
                _totalUnitsInStock += product.UnitsInStock;
            }

            if (!product.IsUnitsOnOrderNull())
            {
                _totalUnitsOnOrder += product.UnitsOnOrder;
            }
        }
        else if (e.Row.RowType == DataControlRowType.Footer)
        {
            // Determine the average UnitPrice
            decimal avgUnitPrice = _totalUnitPrice / (decimal)_totalNonNullUnitPriceCount;

            // Display the summary data in the appropriate cells
            e.Row.Cells[1].Text = "Avg.: " + avgUnitPrice.ToString("c");
            e.Row.Cells[2].Text = "Total: " + _totalUnitsInStock.ToString();
            e.Row.Cells[3].Text = "Total: " + _totalUnitsOnOrder.ToString();
        }
    }
Esempio n. 5
0
 protected void ExpensiveProductsPriceInBoldItalic_DataBound(object sender, EventArgs e)
 {
     // Get the ProductsRow object from the DataItem property...
     Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)ExpensiveProductsPriceInBoldItalic.DataItem).Row;
     if (!product.IsUnitPriceNull() && product.UnitPrice > 75m)
     {
         ExpensiveProductsPriceInBoldItalic.Rows[4].Cells[1].CssClass = "ExpensivePriceEmphasis";
     }
 }
Esempio n. 6
0
 protected void HighlightCheapProducts_RowDataBound(object sender, GridViewRowEventArgs e)
 {
     // Make sure we are working with a DataRow
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
         // Get the ProductsRow object from the DataItem property...
         Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;
         if (!product.IsUnitPriceNull() && product.UnitPrice < 10m)
         {
             e.Row.CssClass = "AffordablePriceEmphasis";
         }
     }
 }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // reference the Delete LinkButton
            LinkButton db = (LinkButton)e.Row.Cells[0].Controls[0];

            // Get information about the product bound to the row
            Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;

            db.OnClientClick = string.Format("return confirm('Are you certain you want to delete the {0} product?');", product.ProductName.Replace("'", @"\'"));
        }
    }
Esempio n. 8
0
 protected string DisplayPrice(Northwind.ProductsRow product)
 {
     // If price is less than $20.00, return the price, highlighted
     if (!product.IsUnitPriceNull() && product.UnitPrice < 20)
     {
         return(string.Concat("<span class=\"AffordablePriceEmphasis\">", product.UnitPrice.ToString("C"), "</span>"));
     }
     else
     {
         // Otherwise return the text, "Please call for a price quote"
         return("<span>Please call for a price quote</span>");
     }
 }
Esempio n. 9
0
    protected void LowStockedProductsInRed_DataBound(object sender, EventArgs e)
    {
        // Get the ProductsRow object from the DataItem property...
        Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)LowStockedProductsInRed.DataItem).Row;
        if (!product.IsUnitsInStockNull() && product.UnitsInStock <= 10)
        {
            Label unitsInStock = (Label)LowStockedProductsInRed.FindControl("UnitsInStockLabel");

            if (unitsInStock != null)
            {
                unitsInStock.CssClass = "LowUnitsInStockEmphasis";
            }
        }
    }
Esempio n. 10
0
    public bool UpdateProduct(string productName, decimal?unitPrice, short?unitsInStock, int productID)
    {
        Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID);
        if (products.Count == 0)
        {
            // no matching record found, return false
            return(false);
        }

        Northwind.ProductsRow product = products[0];

        // Make sure the price hasn't more than doubled
        if (unitPrice != null && !product.IsUnitPriceNull())
        {
            if (unitPrice > product.UnitPrice * 2)
            {
                throw new ApplicationException("When updating a product's price, the new price cannot exceed twice the original price.");
            }
        }


        product.ProductName = productName;
        if (unitPrice == null)
        {
            product.SetUnitPriceNull();
        }
        else
        {
            product.UnitPrice = unitPrice.Value;
        }
        if (unitsInStock == null)
        {
            product.SetUnitsInStockNull();
        }
        else
        {
            product.UnitsInStock = unitsInStock.Value;
        }

        // Update the product record
        int rowsAffected = Adapter.Update(product);

        // Return true if precisely one row was updated, otherwise false
        return(rowsAffected == 1);
    }
Esempio n. 11
0
    protected void ProductsBySupplier_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // Is this a supplier-specific user?
            if (Suppliers.SelectedValue != "-1")
            {
                // Get a reference to the ProductRow
                Northwind.ProductsRow product = (Northwind.ProductsRow)((System.Data.DataRowView)e.Row.DataItem).Row;

                // Is this product discontinued?
                if (product.Discontinued)
                {
                    // Get a reference to the Edit LinkButton
                    LinkButton editButton = (LinkButton)e.Row.Cells[0].Controls[0];

                    // Hide the Edit button
                    editButton.Visible = false;
                }
            }
        }
    }
Esempio n. 12
0
    public bool UpdateProduct(decimal unitPriceAdjustmentPercentage, int productID)
    {
        Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID);
        if (products.Count == 0)
        {
            // no matching record found, return false
            return(false);
        }

        Northwind.ProductsRow product = products[0];

        // Adjust the UnitPrice by the specified percentage (if it's not NULL)
        if (!product.IsUnitPriceNull())
        {
            product.UnitPrice *= unitPriceAdjustmentPercentage;
        }

        // Update the product record
        int rowsAffected = Adapter.Update(product);

        // Return true if precisely one row was updated, otherwise false
        return(rowsAffected == 1);
    }
Esempio n. 13
0
    public bool UpdateProduct(string productName, int?categoryID, int?supplierID, bool discontinued, int productID)
    {
        Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID);
        if (products.Count == 0)
        {
            // no matching record found, return false
            return(false);
        }

        Northwind.ProductsRow product = products[0];

        product.ProductName = productName;
        if (supplierID == null)
        {
            product.SetSupplierIDNull();
        }
        else
        {
            product.SupplierID = supplierID.Value;
        }
        if (categoryID == null)
        {
            product.SetCategoryIDNull();
        }
        else
        {
            product.CategoryID = categoryID.Value;
        }
        product.Discontinued = discontinued;

        // Update the product record
        int rowsAffected = Adapter.Update(product);

        // Return true if precisely one row was updated, otherwise false
        return(rowsAffected == 1);
    }
Esempio n. 14
0
    public bool AddProduct(string productName, int?supplierID, int?categoryID, string quantityPerUnit,
                           decimal?unitPrice, short?unitsInStock, short?unitsOnOrder, short?reorderLevel,
                           bool discontinued)
    {
        // Create a new ProductRow instance
        Northwind.ProductsDataTable products = new Northwind.ProductsDataTable();
        Northwind.ProductsRow       product  = products.NewProductsRow();

        product.ProductName = productName;
        if (supplierID == null)
        {
            product.SetSupplierIDNull();
        }
        else
        {
            product.SupplierID = supplierID.Value;
        }
        if (categoryID == null)
        {
            product.SetCategoryIDNull();
        }
        else
        {
            product.CategoryID = categoryID.Value;
        }
        if (quantityPerUnit == null)
        {
            product.SetQuantityPerUnitNull();
        }
        else
        {
            product.QuantityPerUnit = quantityPerUnit;
        }
        if (unitPrice == null)
        {
            product.SetUnitPriceNull();
        }
        else
        {
            product.UnitPrice = unitPrice.Value;
        }
        if (unitsInStock == null)
        {
            product.SetUnitsInStockNull();
        }
        else
        {
            product.UnitsInStock = unitsInStock.Value;
        }
        if (unitsOnOrder == null)
        {
            product.SetUnitsOnOrderNull();
        }
        else
        {
            product.UnitsOnOrder = unitsOnOrder.Value;
        }
        if (reorderLevel == null)
        {
            product.SetReorderLevelNull();
        }
        else
        {
            product.ReorderLevel = reorderLevel.Value;
        }
        product.Discontinued = discontinued;

        // Add the new product
        products.AddProductsRow(product);
        int rowsAffected = Adapter.Update(products);

        // Return true if precisely one row was inserted, otherwise false
        return(rowsAffected == 1);
    }
Esempio n. 15
0
    public bool UpdateProduct(string productName, int?supplierID, int?categoryID, string quantityPerUnit,
                              decimal?unitPrice, short?unitsInStock, short?unitsOnOrder, short?reorderLevel,
                              bool discontinued, int productID)
    {
        Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID);
        if (products.Count == 0)
        {
            // no matching record found, return false
            return(false);
        }

        Northwind.ProductsRow product = products[0];

        // Business rule check - cannot discontinue a product that's supplied by only
        // one supplier
        if (discontinued)
        {
            // Get the products we buy from this supplier
            Northwind.ProductsDataTable productsBySupplier = Adapter.GetProductsBySupplierID(product.SupplierID);

            if (productsBySupplier.Count == 1)
            {
                // this is the only product we buy from this supplier
                throw new ApplicationException("You cannot mark a product as discontinued if its the only product purchased from a supplier");
            }
        }

        product.ProductName = productName;
        if (supplierID == null)
        {
            product.SetSupplierIDNull();
        }
        else
        {
            product.SupplierID = supplierID.Value;
        }
        if (categoryID == null)
        {
            product.SetCategoryIDNull();
        }
        else
        {
            product.CategoryID = categoryID.Value;
        }
        if (quantityPerUnit == null)
        {
            product.SetQuantityPerUnitNull();
        }
        else
        {
            product.QuantityPerUnit = quantityPerUnit;
        }
        if (unitPrice == null)
        {
            product.SetUnitPriceNull();
        }
        else
        {
            product.UnitPrice = unitPrice.Value;
        }
        if (unitsInStock == null)
        {
            product.SetUnitsInStockNull();
        }
        else
        {
            product.UnitsInStock = unitsInStock.Value;
        }
        if (unitsOnOrder == null)
        {
            product.SetUnitsOnOrderNull();
        }
        else
        {
            product.UnitsOnOrder = unitsOnOrder.Value;
        }
        if (reorderLevel == null)
        {
            product.SetReorderLevelNull();
        }
        else
        {
            product.ReorderLevel = reorderLevel.Value;
        }
        product.Discontinued = discontinued;

        // Update the product record
        int rowsAffected = Adapter.Update(product);

        // Return true if precisely one row was updated, otherwise false
        return(rowsAffected == 1);
    }
Esempio n. 16
0
    public bool UpdateProduct(string productName, int?supplierID, int?categoryID,
                              string quantityPerUnit, decimal?unitPrice, short?unitsInStock,
                              short?unitsOnOrder, short?reorderLevel, bool discontinued, int productID)
    {
        Northwind.ProductsDataTable products = Adapter.GetProductByProductID(productID);
        if (products.Count == 0)
        {
            // no matching record found, return false
            return(false);
        }

        Northwind.ProductsRow product = products[0];

        product.ProductName = productName;
        if (supplierID == null)
        {
            product.SetSupplierIDNull();
        }
        else
        {
            product.SupplierID = supplierID.Value;
        }
        if (categoryID == null)
        {
            product.SetCategoryIDNull();
        }
        else
        {
            product.CategoryID = categoryID.Value;
        }
        if (quantityPerUnit == null)
        {
            product.SetQuantityPerUnitNull();
        }
        else
        {
            product.QuantityPerUnit = quantityPerUnit;
        }
        if (unitPrice == null)
        {
            product.SetUnitPriceNull();
        }
        else
        {
            product.UnitPrice = unitPrice.Value;
        }
        if (unitsInStock == null)
        {
            product.SetUnitsInStockNull();
        }
        else
        {
            product.UnitsInStock = unitsInStock.Value;
        }
        if (unitsOnOrder == null)
        {
            product.SetUnitsOnOrderNull();
        }
        else
        {
            product.UnitsOnOrder = unitsOnOrder.Value;
        }
        if (reorderLevel == null)
        {
            product.SetReorderLevelNull();
        }
        else
        {
            product.ReorderLevel = reorderLevel.Value;
        }
        product.Discontinued = discontinued;

        // Update the product record
        int rowsAffected = Adapter.Update(product);

        // Return true if precisely one row was updated,
        // otherwise false
        return(rowsAffected == 1);
    }