using System; using System.Web.UI; using Northwind45.BusinessObject; using System.Web.Services; namespace Northwind45 { public partial class AddEditProducts : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string operation = (string)Request["operation"]; if (!String.IsNullOrEmpty(operation)) { DdlSupplierID.DataSource = Suppliers.SelectSuppliersDropDownListData(); DdlSupplierID.DataBind(); DdlCategoryID.DataSource = Categories.SelectCategoriesDropDownListData(); DdlCategoryID.DataBind(); if (operation == "update") { LblTitle.Text = "Edit Products"; BtnUpdateRecord.Visible = true; BtnAddRecord.Visible = false; // retrieve id(s) to be updated string productID = (string)Request["productid"]; if (!String.IsNullOrEmpty(productID)) { // retrieve record to be updated Products objProducts = Northwind45.BusinessObject.Products.SelectByPrimaryKey(Convert.ToInt32(productID)); if (objProducts != null) { TxtProductID.Enabled = false; TxtProductID.Text = objProducts.ProductID.ToString(); TxtProductName.Text = objProducts.ProductName; DdlSupplierID.SelectedValue = objProducts.SupplierID.ToString(); DdlCategoryID.SelectedValue = objProducts.CategoryID.ToString(); TxtQuantityPerUnit.Text = objProducts.QuantityPerUnit; CbxDiscontinued.Checked = objProducts.Discontinued; if(objProducts.UnitPrice != null) TxtUnitPrice.Text = objProducts.UnitPrice.ToString(); if(objProducts.UnitsInStock != null) TxtUnitsInStock.Text = objProducts.UnitsInStock.ToString(); if(objProducts.UnitsOnOrder != null) TxtUnitsOnOrder.Text = objProducts.UnitsOnOrder.ToString(); if(objProducts.ReorderLevel != null) TxtReorderLevel.Text = objProducts.ReorderLevel.ToString(); } } } else { // add a record PnlPrimaryKey.Visible = false; BtnUpdateRecord.Visible = false; } } } } protected void BtnAddRecord_Click(object sender, EventArgs e) { AddOrUpdateRecord("add"); } protected void BtnUpdateRecord_Click(object sender, EventArgs e) { AddOrUpdateRecord("update"); } private void AddOrUpdateRecord(string operation) { if (IsValid) { Products objProducts; if (operation == "update") objProducts = Northwind45.BusinessObject.Products.SelectByPrimaryKey(Convert.ToInt32(TxtProductID.Text)); else { objProducts = new Products(); } objProducts.ProductName = TxtProductName.Text; objProducts.Discontinued = CbxDiscontinued.Checked; if (String.IsNullOrEmpty(DdlSupplierID.SelectedValue)) objProducts.SupplierID = null; else objProducts.SupplierID = Convert.ToInt32(DdlSupplierID.SelectedValue); if (String.IsNullOrEmpty(DdlCategoryID.SelectedValue)) objProducts.CategoryID = null; else objProducts.CategoryID = Convert.ToInt32(DdlCategoryID.SelectedValue); if (String.IsNullOrEmpty(TxtQuantityPerUnit.Text)) objProducts.QuantityPerUnit = null; else objProducts.QuantityPerUnit = TxtQuantityPerUnit.Text; if (String.IsNullOrEmpty(TxtUnitPrice.Text)) objProducts.UnitPrice = null; else objProducts.UnitPrice = Convert.ToDecimal(TxtUnitPrice.Text); if (String.IsNullOrEmpty(TxtUnitsInStock.Text)) objProducts.UnitsInStock = null; else objProducts.UnitsInStock = Convert.ToInt16(TxtUnitsInStock.Text); if (String.IsNullOrEmpty(TxtUnitsOnOrder.Text)) objProducts.UnitsOnOrder = null; else objProducts.UnitsOnOrder = Convert.ToInt16(TxtUnitsOnOrder.Text); if (String.IsNullOrEmpty(TxtReorderLevel.Text)) objProducts.ReorderLevel = null; else objProducts.ReorderLevel = Convert.ToInt16(TxtReorderLevel.Text); // the insert method returns the newly created primary key int newlyCreatedPrimaryKey; bool isNoErrors = true; try { if (operation == "update") objProducts.Update(); else newlyCreatedPrimaryKey = objProducts.Insert(); } catch (Exception ex) { if (operation == "update") Functions.ShowModalError(ex, this, "An rror occured on item update!"); else Functions.ShowModalError(ex, this, "An error occured on item insertion!"); isNoErrors = false; } if(isNoErrors) Response.Redirect("~/GridViewProducts.aspx"); } } } }