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); }
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); }
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; }
protected void Page_Load(object sender, EventArgs e) { NorthwindTableAdapters.ProductsTableAdapter productsAdapter = new NorthwindTableAdapters.ProductsTableAdapter(); Northwind.ProductsDataTable products = productsAdapter.GetProducts(); foreach (Northwind.ProductsRow product in products) { Response.Write("Product: " + product.ProductName + "<br />"); } }
protected Northwind.ProductsDataTable GetProductsInCategory(int categoryID) { // First, see if we've yet to have accessed all of the product information if (allProducts == null) { ProductsBLL productAPI = new ProductsBLL(); allProducts = productAPI.GetProducts(); } // Return the filtered view allProducts.DefaultView.RowFilter = "CategoryID = " + categoryID; return allProducts; }
protected Northwind.ProductsDataTable GetProductsInCategory(int categoryID) { // First, see if we've yet to have accessed all of the product information if (allProducts == null) { ProductsBLL productAPI = new ProductsBLL(); allProducts = productAPI.GetProducts(); } // Return the filtered view allProducts.DefaultView.RowFilter = "CategoryID = " + categoryID; return(allProducts); }
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); }
protected void Page_Load(object sender, EventArgs e) { NorthwindTableAdapters.ProductsTableAdapter productsAdapter = new NorthwindTableAdapters.ProductsTableAdapter(); // For each product, double its price if it is not discontinued and // there are 25 items in stock or less Northwind.ProductsDataTable products = productsAdapter.GetProducts(); foreach (Northwind.ProductsRow product in products) { if (!product.Discontinued && product.UnitsInStock <= 25) { product.UnitsInStock *= 2; } } // Update the products productsAdapter.Update(products); }
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); }
protected void Page_Load(object sender, EventArgs e) { NorthwindTableAdapters.SuppliersTableAdapter suppliersAdapter = new NorthwindTableAdapters.SuppliersTableAdapter(); // Get all of the suppliers Northwind.SuppliersDataTable suppliers = suppliersAdapter.GetSuppliers(); // Enumerate the suppliers foreach (Northwind.SuppliersRow supplier in suppliers) { Response.Write("Supplier: " + supplier.CompanyName); Response.Write("<ul>"); // List the products for this supplier Northwind.ProductsDataTable products = supplier.GetProducts(); foreach (Northwind.ProductsRow product in products) { Response.Write("<li>" + product.ProductName + "</li>"); } Response.Write("</ul><p> </p>"); } }
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); }
public override SiteMapNode BuildSiteMap() { // Use a lock to make this method thread-safe lock (siteMapLock) { // First, see if we already have constructed the // rootNode. If so, return it... if (root != null) { return(root); } // We need to build the site map! // Clear out the current site map structure base.Clear(); // Get the categories and products information from the database ProductsBLL productsAPI = new ProductsBLL(); Northwind.ProductsDataTable products = productsAPI.GetProducts(); // Create the root SiteMapNode root = new SiteMapNode( this, "root", "~/SiteMapProvider/Default.aspx", "All Categories"); AddNode(root); // Create SiteMapNodes for the categories and products foreach (Northwind.ProductsRow product in products) { // Add a new category SiteMapNode, if needed string categoryKey, categoryName; bool createUrlForCategoryNode = true; if (product.IsCategoryIDNull()) { categoryKey = "Category:None"; categoryName = "None"; createUrlForCategoryNode = false; } else { categoryKey = string.Concat("Category:", product.CategoryID); categoryName = product.CategoryName; } SiteMapNode categoryNode = FindSiteMapNodeFromKey(categoryKey); // Add the category SiteMapNode if it does not exist if (categoryNode == null) { string productsByCategoryUrl = string.Empty; if (createUrlForCategoryNode) { productsByCategoryUrl = "~/SiteMapProvider/ProductsByCategory.aspx?CategoryID=" + product.CategoryID; } categoryNode = new SiteMapNode( this, categoryKey, productsByCategoryUrl, categoryName); AddNode(categoryNode, root); } // Add the product SiteMapNode string productUrl = "~/SiteMapProvider/ProductDetails.aspx?ProductID=" + product.ProductID; SiteMapNode productNode = new SiteMapNode( this, string.Concat("Product:", product.ProductID), productUrl, product.ProductName); AddNode(productNode, categoryNode); } // Add a "dummy" item to the cache using a SqlCacheDependency // on the Products and Categories tables System.Web.Caching.SqlCacheDependency productsTableDependency = new System.Web.Caching.SqlCacheDependency("NorthwindDB", "Products"); System.Web.Caching.SqlCacheDependency categoriesTableDependency = new System.Web.Caching.SqlCacheDependency("NorthwindDB", "Categories"); // Create an AggregateCacheDependency System.Web.Caching.AggregateCacheDependency aggregateDependencies = new System.Web.Caching.AggregateCacheDependency(); aggregateDependencies.Add(productsTableDependency, categoriesTableDependency); // Add the item to the cache specifying a callback function HttpRuntime.Cache.Insert( CacheDependencyKey, DateTime.Now, aggregateDependencies, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal, new CacheItemRemovedCallback(OnSiteMapChanged)); // Finally, return the root node return(root); } }
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); }
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); }
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); }