protected void getFoodLists() { // connect to the database var conn = new foodlistEntities(); // run the query var FoodLists = from d in conn.FoodLists select d; // display the resulted query in a gridview grdFoodList.DataSource = FoodLists.ToList(); grdFoodList.DataBind(); }
protected void Page_Load(object sender, EventArgs e) { if (IsPostBack == false) { if (!String.IsNullOrEmpty(Request.QueryString["FoodListID"])) { // get the id from the url Int32 FoodListID = Convert.ToInt32(Request.QueryString["FoodListID"]); // connect to the server var conn = new foodlistEntities(); // look up the selected area var objFood = (from d in conn.FoodLists where d.FoodListID == FoodListID select d).FirstOrDefault(); //populate the new change from the form txtName.Text = objFood.Name; txtAmount.Text = objFood.Amount.ToString(); } } }
protected void grdItem_RowDeleting(object sender, GridViewDeleteEventArgs e) { // find out what row is to be deleted Int32 gridindex = e.RowIndex; // find the ID value in said selected row Int32 FoodListID = Convert.ToInt32(grdFoodList.DataKeys[gridindex].Value); // connect to the database var conn = new foodlistEntities(); // delete the selected item FoodList d = new FoodList(); d.FoodListID = FoodListID; conn.FoodLists.Attach(d); conn.FoodLists.Remove(d); conn.SaveChanges(); // refresh the list yummy food ;) getFoodLists(); }
protected void btnSave_Click(object sender, EventArgs e) { // check to see if adding or editing Int32 FoodListID = 0; if (!String.IsNullOrEmpty(Request.QueryString["FoodListID"])) { FoodListID = Convert.ToInt32(Request.QueryString["FoodListID"]); } // connect to the data var conn = new foodlistEntities(); // use the food list class to make new food list item in the list FoodList d = new FoodList(); //fill the properties of the new food item d.Name = txtName.Text; d.Amount = Convert.ToInt32(txtAmount.Text); // save the new food item to the databse if (FoodListID == 0) { conn.FoodLists.Add(d); } else { d.FoodListID = FoodListID; conn.FoodLists.Attach(d); conn.Entry(d).State = System.Data.Entity.EntityState.Modified; } conn.SaveChanges(); // Redirect the user back to the list page Response.Redirect("list.aspx"); }