protected void btnUpdate_Click(object sender, EventArgs e) { if (IsValidUpdate()) { using (MSIPortalContext ctx = new MSIPortalContext()) { LU_tbl_Country countryToBeUpdate = (LU_tbl_Country)Session["LU_tbl_Country_ToEdit"]; countryToBeUpdate.CountryName = txtCountryForEdit.Text; if (((tbl_User)Session["User"]) != null) { countryToBeUpdate.EditUser = ((tbl_User)Session["User"]).UserID; // User } countryToBeUpdate.EditDate = DateTime.Now; ctx.LU_tbl_Country.Attach(countryToBeUpdate); ctx.Entry(countryToBeUpdate).State = System.Data.Entity.EntityState.Modified; ctx.SaveChanges(); this.BindGrid(); TabContainer1.ActiveTabIndex = 1; TabPanelEditCountry.Visible = false; } } }
protected void btnSave_Click(object sender, EventArgs e) { if (IsValid()) { using (MSIPortalContext ctx = new MSIPortalContext()) { try { LU_tbl_Country country = new LU_tbl_Country(); var maxId = ctx.LU_tbl_Country.Select(c => c.CountryID).Max(); // Select Max Id int newId = Convert.ToInt32(maxId) + 1; string newStringId = newId.ToString("D3"); country.CountryID = newStringId; country.CountryName = txtCountry.Text.Trim(); country.EditUser = ((tbl_User)Session["User"]).UserID; country.EditDate = DateTime.Now; ctx.LU_tbl_Country.Add(country); ctx.SaveChanges(); lblSuccessMessage.Text = "Country added successfully"; lblErrorMessage.Text = string.Empty; txtCountry.Text = string.Empty; txtCountry.Focus(); MessagePanel.Visible = true; } catch (Exception ex) { } } } }
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { TabPanelEditCountry.Visible = true; TabContainer1.ActiveTabIndex = 3; if (e.CommandName == "Modify") { string argument = e.CommandArgument.ToString(); using (MSIPortalContext ctx = new MSIPortalContext()) { string countryId = argument; var country = from c in ctx.LU_tbl_Country where c.CountryID == countryId select c; LU_tbl_Country objCountry = country.ToList <LU_tbl_Country>().FirstOrDefault <LU_tbl_Country>(); Session["LU_tbl_Country_ToEdit"] = objCountry; this.txtCountryForEdit.Text = objCountry.CountryName; TabPanelEditCountry.Visible = true; this.lblSuccessMessage.Text = string.Empty; this.lblErrorMessage.Text = string.Empty; } } }