protected void GetItem() { try { //populate form with existing student record Int32 BoughtItemID = Convert.ToInt32(Request.QueryString["BoughtItemID"]); //connect to db via EF using (DefaultConnectionEF db = new DefaultConnectionEF()) { BoughtItem i = (from objS in db.BoughtItems where objS.BoughtItemID == BoughtItemID select objS).FirstOrDefault(); if (i != null) { txtItemName.Text = i.ItemName; txtItemDescription.Text = i.ItemDescription; txtCost.Text = i.BoughtCost.ToString(); ItemID.Text = i.BoughtItemID.ToString(); } } } catch { Response.Redirect("error.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { try { //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { //make a new boughtitem object in memory SoldItem s = new SoldItem(); //fill the properties of our item object from the form inputs s.ItemName = txtItemName.Text; s.Description = txtItemDescription.Text; s.SoldCost = Convert.ToDecimal(txtCost.Text); s.Profit = Convert.ToDecimal(txtPrice.Text) - Convert.ToDecimal(txtCost.Text); s.BoughtItemID = Convert.ToInt32(ItemID.Text); if (Request.QueryString.Count == 0) { conn.SoldItems.Add(s); } conn.SaveChanges(); //redirect to inventory page Response.Redirect("inventory.aspx"); } } catch { Response.Redirect("error.aspx"); } }
protected void getProfit() { try { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { var s = (from objS in conn.SoldItems where objS.SoldItemID > 0 select objS.Profit) .AsEnumerable() .Sum(x => Convert.ToDouble(x)); txtProfit.Text = s.ToString(); } } catch { Response.Redirect("error.aspx"); } }
protected void GetInventory() { try { using (DefaultConnectionEF conn = new DefaultConnectionEF()) { var item = from d in conn.BoughtItems select d; //bind the result to the gridview grdInventory.DataSource = item.ToList(); grdInventory.DataBind(); } } catch { Response.Redirect("error.aspx"); } }
protected void delete() { try { Int32 BoughtItemID = Convert.ToInt32(Request.QueryString["BoughtItemID"]); //connect using (DefaultConnectionEF conn = new DefaultConnectionEF()) { var items = (from item in conn.BoughtItems where item.BoughtItemID == BoughtItemID select item); var cnt = items.Count(); BoughtItem i = (items).FirstOrDefault(); //delete record conn.BoughtItems.Remove(i); conn.SaveChanges(); } } catch { Response.Redirect("error.aspx"); } }