protected void btnInsert_Click(object sender, EventArgs e) { foreach (GridViewRow row in gvToppings.Rows) { if (row.Cells[2].Text.ToLower().Equals(tbToppingName.Text.ToLower())) { if (!ClientScript.IsClientScriptBlockRegistered("script")) { ClientScript.RegisterStartupScript(this.GetType(), "script", "<script language = 'javascript'>alert('Topping already exists')</script>"); } return; } } if (tbToppingName.Text != String.Empty) { Topping topping = new Topping(); topping.name = tbToppingName.Text; topping.active = "yes"; using (MaryPizzaEntities ctx = new MaryPizzaEntities()) { ctx.Toppings.Add(topping); ctx.SaveChanges(); gvToppings.DataBind(); if (!ClientScript.IsClientScriptBlockRegistered("script")) { ClientScript.RegisterStartupScript(this.GetType(), "script", "<script language = 'javascript'>alert('Topping successfully created!')</script>"); } } } }
// The id parameter name should match the DataKeyNames value set on the control public void gvToppings_DeleteItem(int id) { using(MaryPizzaEntities ctx = new MaryPizzaEntities()){ Topping topping = (from t in ctx.Toppings where t.toppingId == id select t).Single(); topping.active = "no"; ctx.SaveChanges(); } }
protected void btnOrder_Click(object sender, EventArgs e) { string delivery; if (rbDelivery.Checked) { delivery = "Delivery"; } else { delivery = "Pick Up"; } using (MaryPizzaEntities ctx = new MaryPizzaEntities()) { Order order = new Order(); foreach (ListItem item in lbToppings.Items) { if (item.Selected) { order.Toppings.Add( ctx.Toppings.Where(t => t.name == item.Text).Select(t => t).First()); } } order.delivery = delivery; order.firstname = tbFirst.Text; order.lastName = tbLast.Text; order.email = tbEmail.Text; order.phone = tbPhone.Text; order.size = ddlSize.SelectedValue; order.noOfPizzas = Int32.Parse(tbQuantity.Text); order.orderDate = DateTime.Now; ctx.Orders.Add(order); ctx.SaveChanges(); if (!ClientScript.IsClientScriptBlockRegistered("script")) { String str = "Order Submitted!\\n" + order.lastName + ", " + order.firstname + "\\n" + "Phone: " + order.phone + "\\n" + order.email + "\\n" + "Size: " + order.size + "\\n" + "Quantity: " + order.noOfPizzas + "\\n" + order.email + "\\n" + order.delivery + "\\n"; ClientScript.RegisterStartupScript(this.GetType(), "script", "<script language = 'javascript'>if(confirm('" + str + "')){ window.location.href = 'Orders.aspx';} else {window.location.href = 'Orders.aspx';}</script>"); } } }
protected void btnGetReport_Click(object sender, EventArgs e) { DateTime start = calendarStart.SelectedDate; DateTime end = calendarEnd.SelectedDate; using(MaryPizzaEntities ctx = new MaryPizzaEntities()) { var query = (from o in ctx.Orders where o.orderDate >= start && o.orderDate <= end select o).ToList(); gvReports.DataSource = query; gvReports.DataBind(); } }
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { using (MaryPizzaEntities ctx = new MaryPizzaEntities()) { var toppings = (from c in ctx.Toppings select c).ToList(); lbToppings.DataSource = toppings; lbToppings.DataTextField = "name"; lbToppings.DataValueField = "toppingid"; lbToppings.DataBind(); } } }