protected void intro_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "EDT") { int rowIndex = Convert.ToInt32(e.CommandArgument); GridView_intro.SelectedIndex = rowIndex; hdnID.Value = ((HiddenField)GridView_intro.Rows[rowIndex].FindControl("hdnID")).Value; txtfname.Text = ((Label)GridView_intro.Rows[rowIndex].FindControl("lblFirstName")).Text; txtlname.Text = ((Label)GridView_intro.Rows[rowIndex].FindControl("lblLastName")).Text; txtage.Text = ((Label)GridView_intro.Rows[rowIndex].FindControl("lblAge")).Text; txtgender.Text = ((Label)GridView_intro.Rows[rowIndex].FindControl("lblGender")).Text; txtemail.Text = ((Label)GridView_intro.Rows[rowIndex].FindControl("lblEmail")).Text; txtphonenumber.Text = ((Label)GridView_intro.Rows[rowIndex].FindControl("lblPhoneNumber")).Text; } else if (e.CommandName == "DLT") { int rowIndex = Convert.ToInt32(e.CommandArgument); hdnID.Value = ((HiddenField)GridView_intro.Rows[rowIndex].FindControl("hdnID")).Value; using (dbfirst_databaseEntities context = new dbfirst_databaseEntities()) { Int64 ID = Convert.ToInt64(hdnID.Value); Intro abc = context.Introes.Where(m => m.ID == ID).SingleOrDefault(); context.Introes.Remove(abc); if (context.SaveChanges() > 0) { GridView_intro.SelectedIndex = -1; BindGrid(); ClearControls(); Page.ClientScript.RegisterStartupScript(this.GetType(), "Deleted", "<script>alert('Deleted successfully.');</script>"); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "Deleted", "<script>alert('Error while deleting.');</script>"); } } } }
protected void btnsave_Click(object sender, EventArgs e) { if (Page.IsValid) { using (dbfirst_databaseEntities context = new dbfirst_databaseEntities()) { Intro obj = new Intro(); obj.ID = Convert.ToInt64(hdnID.Value); obj.FirstName = txtfname.Text.Trim(); obj.LastName = txtlname.Text.Trim(); obj.Age = Convert.ToInt16(txtage.Text.Trim()); obj.Gender = txtgender.Text.Trim(); obj.Email = txtemail.Text.Trim(); obj.PhoneNumber = Convert.ToInt64(txtphonenumber.Text.Trim()); if (Convert.ToInt64(hdnID.Value) > 0) { context.Introes.Attach(obj); var entry = context.Entry(obj); context.Configuration.ValidateOnSaveEnabled = false; context.Entry(obj).Property(u => u.FirstName).IsModified = true; context.Entry(obj).Property(u => u.LastName).IsModified = true; context.Entry(obj).Property(u => u.Age).IsModified = true; context.Entry(obj).Property(u => u.Gender).IsModified = true; context.Entry(obj).Property(u => u.Email).IsModified = true; context.Entry(obj).Property(u => u.PhoneNumber).IsModified = true; if (context.SaveChanges() > 0) { context.Configuration.ValidateOnSaveEnabled = true; GridView_intro.SelectedIndex = -1; BindGrid(); ClearControls(); Page.ClientScript.RegisterStartupScript(this.GetType(), "Saved", "<script>alert('Updated successfully.');</script>"); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "Saved", "<script>alert('Error while updating.');</script>"); } } else { context.Introes.Add(obj); if (context.SaveChanges() > 0) { var intro_ID = obj.ID; GridView_intro.SelectedIndex = -1; BindGrid(); ClearControls(); Page.ClientScript.RegisterStartupScript(this.GetType(), "Saved", "<script>alert('Saved successfully.');</script>"); } else { Page.ClientScript.RegisterStartupScript(this.GetType(), "Saved", "<script>alert('Error while saving.');</script>"); } } } } }