public static Defendant CreateCurrentDefendant(SqlDataReader dr) { Defendant defendant = new Defendant(); int id; string updatedby; DateTime?updateddate; if (!dr.IsClosed && dr.HasRows) { // getting id that uniquely identifies the object id = Convert.ToInt32(dr["defendantid"]); updatedby = dr["updatedby"].ToString(); updateddate = (DateTime?)dr["updateddate"]; defendant = new Defendant(id, updatedby, updateddate); defendant.RaiseChangedEvents = false; defendant.FirstName = dr["firstname"].ToString(); defendant.MiddleName = dr["middlename"].ToString(); defendant.LastName = dr["lastname"].ToString(); defendant.Aka = dr["aka"].ToString(); defendant.SSN = dr["ssn"].ToString(); if (!dr.IsDBNull(dr.GetOrdinal("birthdate"))) { defendant.BirthDate = dr["birthdate"].ToString(); } defendant.DriversLicense = dr["driverslicense"].ToString(); defendant.Street1 = dr["street1"].ToString(); defendant.Street2 = dr["street2"].ToString(); defendant.City = dr["city"].ToString(); defendant.StateID = (dr.IsDBNull(dr.GetOrdinal("stateid"))) ? -1 : Convert.ToInt32(dr["stateid"].ToString()); defendant.Zip = dr["zip"].ToString(); defendant.HomePhone = dr["phonehome"].ToString(); defendant.MobilePhone = dr["phonemobile"].ToString(); defendant.HasProbationOfficer = (dr.IsDBNull(dr.GetOrdinal("hasprobationofficer"))) ? false : Convert.ToBoolean(dr["hasprobationofficer"].ToString()); defendant.ProbationOfficer = dr["probationofficer"].ToString(); if (!dr.IsDBNull(dr.GetOrdinal("barreduntil"))) { defendant.BarredUntil = dr["barreduntil"].ToString(); } defendant.Notes = dr["notes"].ToString(); if (!dr.IsDBNull(dr.GetOrdinal("active"))) { defendant.Active = Convert.ToBoolean(dr["active"].ToString()); } defendant.Save(false); defendant.RaiseChangedEvents = true; } return(defendant); }
private bool CanChangeCurrentDefendant() { bool rtnValue = true; if (bindingDefendants.Current != null) { Defendant defendant = (Defendant)bindingDefendants.Current; switch (defendant.MyState) { case MyObjectState.New: case MyObjectState.Modified: switch (MyMessageBox.Show(this, "Defendant", MyDisplayMessage.SaveConfirm)) { case DialogResult.Yes: try { defendant.Save(true); bindingDefendants.ResetCurrentItem(); rtnValue = true; } catch (MyException ex) { MyMessageBox.Show(this, "Defendant", MyDisplayMessage.SaveError, ex); rtnValue = false; } break; case DialogResult.No: if (defendant.MyState == MyObjectState.New) { bindingDefendants.RemoveCurrent(); bindingDefendants.ResetBindings(false); ucDefendant.Focus(); } else { defendant.Reset(); } rtnValue = true; break; case DialogResult.Cancel: rtnValue = false; break; } break; } } return(rtnValue); }
private void mnuDefendant_Click(object sender, EventArgs e) { Defendant defendant = (Defendant)bindingDefendants.Current; /******************************************************************************** * New ********************************************************************************/ if (sender.Equals(mnuDefendantNew)) { if (CanChangeCurrentDefendant()) { this.SuspendLayout(); if (ucFilter.FilterValue.Length > 0) { bindingDefendants.Filter = ""; ucFilter.FilterValue = ""; } _GridViewState = GridViewState.Adding; bindingDefendants.AddNew(); _GridViewState = GridViewState.None; bindingDefendants.ResetCurrentItem(); this.ResumeLayout(false); ucDefendant.Focus(); } } /******************************************************************************** * Remove ********************************************************************************* * else if( sender.Equals( mnuDefendantRemove ) ) * { * if( DialogResult.OK == MyMessageBox.Show( this, "Defendant", MyDisplayMessage.RemoveConfirm ) ) * { * this.SuspendLayout(); * * try * { * bindingDefendants.RemoveCurrent(); * bindingDefendants.ResetBindings( false ); * this.ucFilter.Focus(); * } * catch( MyException ex ) * { * MyMessageBox.Show( this, "Defendant", MyDisplayMessage.RemoveError, ex ); * } * * bindingDefendants.ResetCurrentItem(); * * this.ResumeLayout( false ); * } * * }*/ /******************************************************************************** * Refresh ********************************************************************************/ else if (sender.Equals(mnuDefendantRefresh)) { if (!defendant.MyState.Equals(MyObjectState.Current)) { if (DialogResult.Cancel == MyMessageBox.Show(this, "Defendant", MyDisplayMessage.RefreshConfirm)) { return; } } this.Cursor = Cursors.WaitCursor; this.SuspendLayout(); ucDefendant.SuspendLayout(); try { defendant.Refresh(); bindingDefendants.ResetBindings(false); ucDefendant.Update(); ucDefendant.Focus(); } catch (MyException ex) { MyMessageBox.Show(this, "Defendant", MyDisplayMessage.RefreshError, ex); } ucDefendant.ResumeLayout(false); this.ResumeLayout(false); this.Cursor = Cursors.Default; } /******************************************************************************** * Save ********************************************************************************/ else if (sender.Equals(mnuDefendantSave)) { ucDefendant.Select(); bindingDefendants.EndEdit(); this.Cursor = Cursors.WaitCursor; this.SuspendLayout(); try { defendant.Save(true); bindingDefendants.Sort = ""; // setting sort string strSort = string.Empty; string strSortOrder = (dgvDefendants.SortOrder == SortOrder.Descending) ? "DESC" : "ASC"; if (dgvDefendants.SortedColumn.DataPropertyName.ToLower() == "lastname") { strSort = "LastName " + strSortOrder + ", FirstName " + strSortOrder; } else { strSort = "FirstName " + strSortOrder + ", LastName " + strSortOrder; } bindingDefendants.Sort = strSort; ResetDataGridViewDefendant(defendant); } catch (MyException ex) { MyMessageBox.Show(this, "Defendant", MyDisplayMessage.SaveError, ex); } catch (ArgumentOutOfRangeException) { MyMessageBox.Show(this, "Payment Arrangement", MyDisplayMessage.PaymentArrangementOverlapping); } this.ResumeLayout(false); this.Cursor = Cursors.Default; } /******************************************************************************** * Cancel ********************************************************************************/ else if (sender.Equals(mnuDefendantCancel)) { this.SuspendLayout(); if (defendant.MyState == MyObjectState.New) { bindingDefendants.RemoveCurrent(); if (bindingDefendants.Count > 0) { bindingDefendants.Position = 0; ucDefendant.Focus(); } else { this.ucFilter.Focus(); } } else { defendant.Reset(); bindingDefendants.ResetCurrentItem(); } this.ResumeLayout(false); } /******************************************************************************** * Notes ********************************************************************************/ else if (sender.Equals(mnuDefendantNotes)) { if (frmNotes == null) { frmNotes = new frmNotes(); frmNotes.DataBindings.Add("Notes", bindingDefendants, "Notes", true, DataSourceUpdateMode.OnPropertyChanged); frmNotes.FormClosed += new FormClosedEventHandler(frmNotes_FormClosed); } if (!frmNotes.Visible) { frmNotes.Show(this); } else { frmNotes.Focus(); } } /******************************************************************************** * Mail Merge ********************************************************************************/ else if (sender.Equals(mnuDefendantMailMerge)) { if (!MailMerge.StoreDirectoryConfigured() && !MailMerge.StoreDirectoryExists()) { MyMessageBox.Show(this, "MS Word Mail Merge", MyDisplayMessage.MailMergeDirectoryError); return; } frmMailMergeTemplates frm = new frmMailMergeTemplates(); if (DialogResult.Cancel == frm.ShowDialog(this) || string.IsNullOrEmpty(frm.SelectedMailMergeTemplatePath)) { return; } this.Cursor = Cursors.WaitCursor; int defendantid = ((Defendant)bindingDefendants.Current).ID; int planid = ucPlans.PlanId; string templatePath = frm.SelectedMailMergeTemplatePath; try { MailMerge.PerformMailMerge(defendantid, planid, templatePath); } catch (Exception ex) { MyMessageBox.Show(this, "Mail Merge", MyDisplayMessage.MailMergeDocumentMergeError, new MyException(ex.Message.ToString(), MyErrorType.MailMergeError, ex)); } this.Cursor = Cursors.Default; } /******************************************************************************** * Make Payment ********************************************************************************/ else if (sender.Equals(mnuDefendantPayment)) { BindingSource bindingPayments = new BindingSource(new BindingSource(bindingDefendants, "Plans"), "Payments"); frmPayment frm = new frmPayment(ref bindingPayments); if (!frm.IsDisposed) { frm.ShowDialog(this); bindingDefendants.ResetCurrentItem(); } } /******************************************************************************** * Remove/Archive - doesn't delete from the database ********************************************************************************/ else if (sender.Equals(mnuDefendantRemove)) { if (DialogResult.OK == MyMessageBox.Show(this, "Remove Defendant", MyDisplayMessage.Archive)) { this.SuspendLayout(); defendant.Reset(); defendant.RaiseChangedEvents = false; defendant.Active = false; defendant.Save(true); // set state to new to fake out the collection into removing it but not removing it from the db. //defendant.MyState = MyObjectState.New; defendant.RaiseChangedEvents = true; try { bindingDefendants.Remove(defendant); bindingDefendants.ResetBindings(true); this.ucFilter.Focus(); } catch (MyException ex) { MyMessageBox.Show(this, "Defendant", MyDisplayMessage.RemoveError, ex); } this.ResumeLayout(); } } }