private void btnUpdateContact_Click(object sender, EventArgs e)
        {
            Individual IndividualObj = (Individual)individualBindingSource.Current;

            //Data.Models.Company CompanyObj = (Data.Models.Company)companyBindingSource.Current;

            Individual IndividualToUpdate = null;

            foreach (Individual CurrentIndividualObj in CurrentCompany.Individuals)
            {
                System.Type type = IndividualObj.GetType();
                //if (CurrentIndividualObj.IndividualID == (int)type.GetProperty("IndividualID").GetValue(IndividualObj, null))
                if (CurrentIndividualObj.IndividualID == IndividualObj.IndividualID)
                {
                    IndividualToUpdate = CurrentIndividualObj;
                }
            }
            if (IndividualToUpdate != null)
            {
                using (frmContactsAddUpdateContacts frm = new frmContactsAddUpdateContacts(IndividualToUpdate.IndividualID))
                {
                    frm.CurrentContact = IndividualToUpdate;
                    frm.ShowDialog();
                    this.refreshCompanyContacts();
                };
            }
        }
 private void btnAddContact_Click(object sender, EventArgs e)
 {
     using (frmContactsAddUpdateContacts frm = new frmContactsAddUpdateContacts(0))
     {
         frm.IsStudent = true;
         frm.ShowDialog();
         refreshContacts();
     }
 }
Ejemplo n.º 3
0
        private void btnUpdateContact_Click(object sender, EventArgs e)
        {
            if (companyContactsBindingSource.Count > 0)
            {
                CompanyContacts CC          = (CompanyContacts)companyContactsBindingSource.Current;
                Individual      CCFromCache = CurrentlySelectedCompany.Individuals.Where(a => a.IndividualID == CC.IndividualID).First();

                using (frmContactsAddUpdateContacts frm = new frmContactsAddUpdateContacts(CC.IndividualID, CCFromCache))
                {
                    frm.ShowDialog();
                    this.refreshCompanyContacts();
                }
            }
        }
Ejemplo n.º 4
0
        private void btnAddContact_Click(object sender, EventArgs e)
        {
            //CompanyContacts CC = (CompanyContacts)companyContactsBindingSource.Current;
            //Individual CCFromCache = CurrentlySelectedCompany.Individuals.Where(a => a.IndividualID == CC.IndividualID).First();

            using (frmContactsAddUpdateContacts frm = new frmContactsAddUpdateContacts(0))
            {
                frm.ShowDialog();
                if (frm.CurrentContact != null)
                {
                    using (var Dbconnection = new MCDEntities())
                    {
                        using (System.Data.Entity.DbContextTransaction dbTran = Dbconnection.Database.BeginTransaction())
                        {
                            try
                            {
                                //CRUD Operations

                                /*
                                 * this steps follow to both entities
                                 *
                                 * 1 - create instance of entity with relative primary key
                                 *
                                 * 2 - add instance to context
                                 *
                                 * 3 - attach instance to context
                                 */

                                // 1
                                Data.Models.Company C = new Data.Models.Company {
                                    CompanyID = CurrentlySelectedCompany.CompanyID
                                };
                                // 2
                                Dbconnection.Companies.Add(C);
                                // 3
                                Dbconnection.Companies.Attach(C);

                                // 1
                                Individual I = new Individual {
                                    IndividualID = frm.CurrentContact.IndividualID
                                };
                                // 2
                                Dbconnection.Individuals.Add(I);
                                // 3
                                Dbconnection.Individuals.Attach(I);

                                // like previous method add instance to navigation property
                                C.Individuals.Add(I);

                                ////saves all above operations within one transaction
                                Dbconnection.SaveChanges();

                                //commit transaction
                                dbTran.Commit();
                                // Dbconnection.Entry(I).Collection(a => a.ContactDetails).Load();
                            }
                            catch (Exception ex)
                            {
                                if (ex is DbEntityValidationException)
                                {
                                    foreach (DbEntityValidationResult entityErr in ((DbEntityValidationException)ex).EntityValidationErrors)
                                    {
                                        foreach (DbValidationError error in entityErr.ValidationErrors)
                                        {
                                            MessageBox.Show(error.ErrorMessage, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                                //Rollback transaction if exception occurs
                                dbTran.Rollback();
                            }
                        }
                    };

                    using (var Dbconnection = new MCDEntities())
                    {
                        Dbconnection.Individuals.Attach(frm.CurrentContact);
                        Dbconnection.Entry(frm.CurrentContact).Reference(a => a.LookupTitle).Load();
                        CurrentlySelectedCompany.Individuals.Add(frm.CurrentContact);
                        this.refreshCompanyContacts();
                    };
                }
            }
        }