protected void dg_ItemCommand(object source, DataGridCommandEventArgs e) { try { if (e.CommandName == "AddANewRow") { var db = new Pizza15Model(); TextBox txtItem = e.Item.FindControl("txtItemF") as TextBox; TextBox txtPrice = e.Item.FindControl("txtPriceF") as TextBox; TextBox txtComment = e.Item.FindControl("txtCommentF") as TextBox; if (txtItem != null && txtPrice != null && txtComment != null) { var RegularSpecials = db.tblRegularSpecials.Select(x => new { RegularID = x.RegularID, Item = x.Item, Price = x.Price, Comment = x.Comment }).ToList(); if (RegularSpecials.Count != 0) { var strQueryMax = db.tblRegularSpecials.Max(x => x.RegularID); int nMaxID = Convert.ToInt32(strQueryMax); var strQuery = new tblRegularSpecial(); strQuery.RegularID = nMaxID + 1; strQuery.Item = txtItem.Text; strQuery.Price = txtPrice.Text; strQuery.Comment = txtComment.Text; db.tblRegularSpecials.Add(strQuery); db.SaveChanges(); } else { var strQueryMax = db.tblRegularSpecials; var strQuery = new tblRegularSpecial(); strQuery.Item = txtItem.Text; strQuery.Price = txtPrice.Text; strQuery.Comment = txtComment.Text; db.tblRegularSpecials.Add(strQuery); db.SaveChanges(); } } else { lblError.Text = "Error finding the Regular Specials"; } Session["RegularSpecial"] = null; GetData(); } } catch (Exception ex) { lblError.Text = ex.Message; } }
protected void dg_UpdateCommand(object source, DataGridCommandEventArgs e) { try { string strID = dg.DataKeys[e.Item.ItemIndex].ToString(); int nStrID = Convert.ToInt32(strID); TextBox txtItem = e.Item.FindControl("txtItemE") as TextBox; TextBox txtPrice = e.Item.FindControl("txtPriceE") as TextBox; TextBox txtComment = e.Item.FindControl("txtCommentE") as TextBox; if (txtItem != null && txtPrice != null && txtComment != null) { var db = new Pizza15Model(); tblRegularSpecial UpdateRegularSpecial = db.tblRegularSpecials.FirstOrDefault(x => x.RegularID.Equals(nStrID)); if (UpdateRegularSpecial != null) { UpdateRegularSpecial.Item = txtItem.Text.Trim(); UpdateRegularSpecial.Price = txtPrice.Text.Trim(); UpdateRegularSpecial.Comment = txtComment.Text.Trim(); db.SaveChanges(); } } else { lblError.Text = "Error finding the Regular Special"; } dg.EditItemIndex = -1; dg.ShowFooter = true; Session["RegularSpecial"] = null; GetData(); } catch (Exception ex) { lblError.Text = ex.Message; } }