protected void Repeater1_OnItemCommand(object source, RepeaterCommandEventArgs e) { if (e.CommandName == "Return") { int MovieID = Convert.ToInt32(((Label)e.Item.FindControl("MovieID")).Text); // Get rented movie from DB so we can return it (remove from rented movies) RentedMovy RM = (from rm in MME.RentedMovies where rm.MovieID == MovieID select rm).First(); //// Remove //MME.RentedMovies.Remove(RM); //// Save //MME.SaveChanges(); //// Look up Movie name so we can display it in confirmation. //string MovieName = ((Label)e.Item.FindControl("MovieName")).Text; //// Display confirmation of return //lblResult.Text = string.Format("Movie '{0}' returned.", MovieName); } }
protected void btnRentMovie_Click(object sender, EventArgs e) { using (var ctx = new MediaEntities()) { int ID = Convert.ToInt32(Request.QueryString["MovieID"].ToString()); Movy mov = (from m in ctx.Movies where m.MovieId == ID select m).First(); foreach (Customer C in ctx.Customers) { ListItem newItem = new ListItem(); newItem.Text = C.CustomerName; newItem.Value = C.CustomerID.ToString(); Customers.Items.Add(newItem); } RentedMovy RM = new RentedMovy(); RM.MovieID = mov.MovieId; RM.CustomerID = Convert.ToInt16(Customers.SelectedValue); try { // Try and confirm to date time. If not show error. RM.RentedTo = DateTime.Parse(RentTo.Text); } catch (Exception ex) { lblResult.Text = "Don't recognize the date format"; return; } ctx.RentedMovies.Add(RM); ctx.SaveChanges(); // Show confirmation to user that movie is saved lblResult.Text = "Movie rented successfully"; } }