protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "InsertNew") { GridViewRow row = GridView1.FooterRow; TextBox txtFirstName = row.FindControl("txtFirstNameNew") as TextBox; TextBox txtLastName = row.FindControl("txtLastNameNew") as TextBox; TextBox txtEmail = row.FindControl("txtEmailNew") as TextBox; TextBox txtPhone = row.FindControl("txtPhoneNew") as TextBox; DropDownList ddlStatus = row.FindControl("ddlStatusNew") as DropDownList; if (txtFirstName != null && txtLastName != null && txtEmail != null && ddlStatus != null) { using (ProjectTrackerContainer context = new ProjectTrackerContainer()) { Invests invest = new Invests(); invest.FirstName = txtFirstName.Text; invest.LastName = txtLastName.Text; invest.Email = txtEmail.Text; invest.Phone = txtPhone.Text; invest.InvestStatusId = Convert.ToInt32(ddlStatus.SelectedValue); context.Invests.Add(invest); WriteInvest(context, invest); BindGrid(); } } } }
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = GridView1.Rows[e.RowIndex]; Label lblId = row.FindControl("lblId") as Label; TextBox txtFirstName = row.FindControl("txtFirstName") as TextBox; TextBox txtLastName = row.FindControl("txtLastName") as TextBox; TextBox txtEmail = row.FindControl("txtEmail") as TextBox; TextBox txtPhone = row.FindControl("txtPhone") as TextBox; DropDownList ddlStatus = row.FindControl("ddlStatus") as DropDownList; if (txtFirstName != null && txtLastName != null && txtEmail != null && ddlStatus != null) { using (ProjectTrackerContainer context = new ProjectTrackerContainer()) { //int investorId = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value); int investorId = Convert.ToInt32(lblId.Text); Invests invest = context.Invests.First(x => x.Id == investorId); invest.FirstName = txtFirstName.Text; invest.LastName = txtLastName.Text; invest.Email = txtEmail.Text; invest.Phone = txtPhone.Text; invest.InvestStatusId = Convert.ToInt32(ddlStatus.SelectedValue); WriteInvest(context, invest); GridView1.EditIndex = -1; BindGrid(); } } }
private void WriteInvest(ProjectTrackerContainer context, Invests invest) { var result = context.Entry(invest).GetValidationResult(); if (!result.IsValid) { lblMsg.Text = result.ValidationErrors.First().ErrorMessage; } else { context.SaveChanges(); lblMsg.Text = "Saved successfully."; //ClientScript.RegisterClientScriptBlock(GetType(), "Javascript", "<script>alert('Investor Added Successfully!')</script>"); } }
/// <summary> /// Creates new instance of PI. /// /// - If a new PI is being entered, the program checks to see if there is any exisitng PI with the /// same first as last name. If the name is new altogether, a new PI is created into the database /// and an email is sent to the tracking team for review. /// /// - If an existing PI is being saved, the program checks PI already has been reviewed by the /// tracking team. If the tracking team has already reviewed the program, the program halts the /// user from saving the newly entered information. Only the tracking team can make existing /// changes to the PI form after approval. /// </summary> /// <param name="investId">PI Id in database. 0 indicates new PI, otherwise exisiting PI.</param> /// <returns>True if okay to save, False if unable to save.</returns> private bool SetPIValue(int investId) { using (ProjectTrackerContainer db = new ProjectTrackerContainer()) { Invests invest; if (investId == 0) // new PI { if (!TextBoxFirstName.Text.Equals(string.Empty) && !TextBoxLastName.Text.Equals(string.Empty)) { invest = new Invests() { FirstName = TextBoxFirstName.Text.Trim(), LastName = TextBoxLastName.Text.Trim(), Email = TextBoxEmail.Text, Phone = TextBoxPhone.Text, AltEmail = TextBoxAltEmail.Text, AltPhone = TextBoxAltPhone.Text, Notes = txtNotes.InnerText, InvestStatusId = Convert.ToInt32(ddlStatus.SelectedValue), IsApproved = chkApproved.Checked, //IsPilot = chkPilot.Checked }; //check existing PI with same name if (db.Invests.Any(i => i.FirstName == invest.FirstName && i.LastName == invest.LastName)) { return(false); } if (ddlStatus.SelectedItem.Text == "Non-Hawaii Client" || ddlStatus.SelectedItem.Text == "UH Student") { invest.NonHawaiiClient = TextBoxNonHawaii.Text; } TextBox txtNonUHOther = GridViewNonUH.FooterRow.FindControl("txtNonUHOther") as TextBox; invest.NonUHClient = txtNonUHOther.Text; TextBox txtDegreeOther = GridViewDegree.FooterRow.FindControl("txtDegreeOther") as TextBox; invest.OtherDegree = txtDegreeOther.Text; TextBox txtCommunityPartnerOther = GridViewCommunityPartner.FooterRow.FindControl("txtCommunityPartnerOther") as TextBox; invest.OtherCommunityPartner = txtCommunityPartnerOther.Text; db.Invests.Add(invest); db.SaveChanges(); investId = invest.Id; //PageUtility.SendNotificationByEmail("PI", investId, User.Identity.Name); SendNotificationEmail(invest.Id); } } else // existing PI { invest = db.Invests.First(i => i.Id == investId); if (!Page.User.IsInRole("Admin") && invest.IsApproved) { //Response.Write("<script>alert('This PI has been approved, please contact with admin group for any changes.');</script>"); return(false); } else { if (invest != null && !TextBoxFirstName.Text.Equals(string.Empty) && !TextBoxLastName.Text.Equals(string.Empty)) { invest.FirstName = TextBoxFirstName.Text.Trim(); invest.LastName = TextBoxLastName.Text.Trim(); invest.Email = TextBoxEmail.Text; invest.Phone = TextBoxPhone.Text; invest.AltEmail = TextBoxAltEmail.Text; invest.AltPhone = TextBoxAltPhone.Text; invest.Notes = txtNotes.InnerText; invest.InvestStatusId = Convert.ToInt32(ddlStatus.SelectedValue); if (ddlStatus.SelectedItem.Text == "Non-Hawaii Client" || ddlStatus.SelectedItem.Text == "UH Student") { invest.NonHawaiiClient = TextBoxNonHawaii.Text; } TextBox txtNonUHOther = GridViewNonUH.FooterRow.FindControl("txtNonUHOther") as TextBox; invest.NonUHClient = txtNonUHOther.Text; TextBox txtDegreeOther = GridViewDegree.FooterRow.FindControl("txtDegreeOther") as TextBox; invest.OtherDegree = txtDegreeOther.Text; TextBox txtCommunityPartnerOther = GridViewCommunityPartner.FooterRow.FindControl("txtCommunityPartnerOther") as TextBox; invest.OtherCommunityPartner = txtCommunityPartnerOther.Text; invest.IsApproved = chkApproved.Checked; //invest.IsPilot = chkPilot.Checked; db.SaveChanges(); } } } #region update PI affiliations List <int> newAffliIdList = GetInvestJabsomAffil(); List <int> prevAffilIdList = new List <int>(); ICollection <JabsomAffil> jabsomAffils = db.Invests.First(i => i.Id == investId).JabsomAffils; foreach (JabsomAffil affil in jabsomAffils) { prevAffilIdList.Add(affil.Id); } var newNotPrevAffilList = newAffliIdList.Except(prevAffilIdList).ToList(); var prevNotNewAffilList = prevAffilIdList.Except(newAffliIdList).ToList(); if (prevNotNewAffilList.Count > 0) { foreach (var expiredId in prevNotNewAffilList) { //project.ProjectBioStats.First(b => b.BioStats_Id == expiredId).EndDate = DateTime.Parse(_currentDate); var jabsomAffil = jabsomAffils.First(j => j.Id == expiredId); jabsomAffils.Remove(jabsomAffil); } } if (newNotPrevAffilList.Count > 0) { foreach (var newId in newNotPrevAffilList) { var jabsomAffil = db.JabsomAffils.First(j => j.Id == newId); jabsomAffils.Add(jabsomAffil); } } db.SaveChanges(); #endregion } return(true); }