Example #1
0
        private void btnOK_Click(object sender, RoutedEventArgs e)
        {
            bool dataValidated = ValidateBuildingInput();

            if (!dataValidated)
            {
                MessageBox.Show("Error with your data. Please check and try again.", "Save to database", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            if (dbOperation == DBOperation.Add && dataValidated)
            {
                DBLibrary.Building building = new DBLibrary.Building();
                building.BuildingName     = tbxBuildingName.Text;
                building.BuildingNo       = tbxBuildingNo.Text;
                building.BuildingTown     = tbxTown.Text;
                building.BuildingCity     = tbxCity.Text;
                building.BuildingPostCode = tbxPostcode.Text;
                // Added 1 to the slectedindex to match the correct record ID in the database
                building.TypeID = cboBuildingType.SelectedIndex + 1;
                building.AreaID = cboBuildingArea.SelectedIndex + 1;

                Contact contact = new Contact();
                contact.FName   = tbxContactFirstName.Text;
                contact.SName   = tbxContactSurname.Text;
                contact.HouseNo = tbxContactHouseNo.Text;
                contact.Street  = tbxContactStreet.Text;
                contact.Town    = tbxContactTown.Text;
                contact.PhoneNo = tbxContactPhoneNo.Text;

                Document document = new Document();
                document.DocDesc = tbxDocumentDesc.Text;
                document.Path    = tbxDocumentPath.Text;

                int contactSaved = SaveContactRecord(contact);
                if (contactSaved == 1)
                {
                    building.ContactID = contact.ContactID;
                    int documentSaved = SaveDocumentRecord(document);
                    if (documentSaved == 1)
                    {
                        building.DocumentID = document.DocumentID;
                        int buildingSaved = SaveBuildingRecord(building);
                        if (buildingSaved == 1)
                        {
                            ClearAllControls();
                            RefreshBuildingsListView();
                            stkBuildingPanel.Visibility   = Visibility.Collapsed;
                            tabBuildingContact.IsEnabled  = false;
                            tabBuildingDocument.IsEnabled = false;
                            MessageBox.Show("Building record has been saved successfully.", "Save to database", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                }
            }
        }
Example #2
0
        private int SaveBuildingRecord(DBLibrary.Building building)
        {
            int saveSuccess = 0;

            try
            {
                db.Entry(building).State = System.Data.Entity.EntityState.Added;
                saveSuccess = db.SaveChanges();
            }
            catch (Exception)
            {
                MessageBox.Show("Error saving building record.", "Save to database", MessageBoxButton.OK, MessageBoxImage.Error);
            }
            return(saveSuccess);
        }