protected void dg_UpdateCommand(object source, DataGridCommandEventArgs e) { try { string strID = dg.DataKeys[e.Item.ItemIndex].ToString(); int nStrID = Convert.ToInt32(strID); string strKeyword = ""; TextBox txtKeyword = e.Item.FindControl("txtKeywordE") as TextBox; if ((txtKeyword != null)) { strKeyword = txtKeyword.Text; var db = new Pizza15Model(); tblKeyword UpdateKeyWord = db.tblKeywords.FirstOrDefault(x => x.ID.Equals(nStrID)); if (UpdateKeyWord != null) { UpdateKeyWord.Keyword = txtKeyword.Text.Trim(); db.SaveChanges(); } } else { lblError.Text = "Error finding the Keyword"; } dg.EditItemIndex = -1; dg.ShowFooter = true; Session["Keyword"] = null; GetData(); } catch (Exception ex) { lblError.Text = ex.Message; } }
protected void dg_ItemCommand(object source, DataGridCommandEventArgs e) { try { if (e.CommandName == "AddANewRow") { var db = new Pizza15Model(); string strKeyword = ""; TextBox txtKeyword = e.Item.FindControl("txtKeywordF") as TextBox; if ((txtKeyword != null)) { var strQueryMax = db.tblKeywords.Max(x => x.ID); int nMaxID = Convert.ToInt32(strQueryMax); strKeyword = txtKeyword.Text; var strQuery = new tblKeyword(); strQuery.ID = nMaxID + 1; strQuery.Keyword = strKeyword; db.tblKeywords.Add(strQuery); db.SaveChanges(); } else { lblError.Text = "Error finding the Keyword"; } Session["Keyword"] = null; GetData(); } } catch (Exception ex) { lblError.Text = ex.Message; } }