private void savebtn_Click(object sender, EventArgs e)
 {
     using (StaffMemberContext db = new StaffMemberContext())
     {
         String surname    = lnTexBox.Text;
         String firstNames = fnTextBox.Text;
         String ID         = idTextBox.Text;
         String skill      = skillComboBox.SelectedItem.ToString();
         String ambID      = ambComboBox.SelectedItem.ToString();
         int    x;
         if (idTextBox.ReadOnly)
         {
             var idCheck = from j in db.StaffMembers where j.ID == ID select j;
             idCheck.First().GivenNames  = firstNames.Replace(" ", "");
             idCheck.First().Surname     = surname;
             idCheck.First().SkillLevel  = skill;
             idCheck.First().AmbulanceID = ambID;
             db.SaveChanges();
             AmbulanceOfficerForm ambulance = new AmbulanceOfficerForm();
             ambulance.Visible = true;
             Visible           = false;
         }
         else
         {
             if (!(int.TryParse(ID, out x)) || ID.Length != 6)
             {
                 MessageBox.Show("ID must be a number.\n Or the ID length must be of length 6.");
                 idTextBox.Clear();
                 idTextBox.Focus();
             }
             else
             {
                 var idCheck = from j in db.StaffMembers where j.ID == ID select j;
                 if (idCheck.Count() != 0)
                 {
                     MessageBox.Show("ID must be unique.");
                     idTextBox.Clear();
                     idTextBox.Focus();
                 }
                 else
                 {
                     var newMem = new StaffMember {
                         GivenNames = firstNames, Surname = surname, ID = ID, SkillLevel = skill, AmbulanceID = ambID
                     };
                     db.StaffMembers.Add(newMem);
                     db.SaveChanges();
                     AmbulanceOfficerForm ambulance = new AmbulanceOfficerForm();
                     ambulance.Visible = true;
                     Visible           = false;
                 }
             }
         }
     }
 }
Beispiel #2
0
        private void addeditofficersavebutton_Click(object sender, EventArgs e)
        {
            //START VALIDATING ALL THE VALUES
            int           offidvalue;
            StaffMember   stafftosave   = new StaffMember();
            List <string> validofficers = new List <string>();

            //vaidate first name
            if (firstnametextbox.Text.Length == 0)
            {
                MessageBox.Show("'First Names' is a required field");
            }
            else if (surnametextbox.Text.Length == 0)
            {
                MessageBox.Show("'Surname' is a required field");
            }
            else if (!int.TryParse(officeridtextbox.Text, out offidvalue) && (officeridtextbox.Text.Length == 6))
            {
                MessageBox.Show("Officer ID must be a six digit number");
            }
            else if (skillcombobox.SelectedItem == null)
            {
                MessageBox.Show("A skill level must be selected");
            }
            else if (ambulancecombobox.SelectedItem == null)
            {
                MessageBox.Show("An ambulance must be selected");
            }
            else
            {
                using (TableContext db = new TableContext())
                {
                    foreach (var record in db.StaffTable)
                    {
                        if (record.officer_id == offidvalue)
                        {
                            stafftosave = record;
                        }
                    }

                    if (stafftosave == null)
                    {
                        //Add a new record
                        StaffMember a = new StaffMember {
                            officer_id = offidvalue, first_names = firstnametextbox.Text, surname = surnametextbox.Text, skill_level = skillcombobox.SelectedItem.ToString(), ambulance_id = ambulancecombobox.SelectedItem.ToString()
                        };
                        db.StaffTable.Add(a);
                    }
                    else
                    {
                        //Update a record

                        stafftosave.officer_id   = offidvalue;
                        stafftosave.first_names  = firstnametextbox.Text;
                        stafftosave.surname      = surnametextbox.Text;
                        stafftosave.skill_level  = skillcombobox.SelectedItem.ToString();
                        stafftosave.ambulance_id = ambulancecombobox.SelectedItem.ToString();
                    }


                    db.SaveChanges();

                    this.Hide();
                    officerform form = new officerform();
                    form.Closed += (s, args) => this.Close();
                    form.ShowDialog();
                }
            }
        }