//Sends data about candidated to the database public void CandidateListToDatabase() { VotingDatabaseEntities db = new VotingDatabaseEntities(); foreach (Candidate candidate in Candidates) { db.Candidates.Add(candidate); } db.SaveChanges(); }
private void btnVote_Click(object sender, RoutedEventArgs e) { //Present the user with a messagebox to ensure his decision var mb = MessageBox.Show("Are you sure this the person you want to vote for?", "Are you sure?", MessageBoxButton.YesNo, MessageBoxImage.Question); if (mb == MessageBoxResult.Yes) { try { //If affirmative, find checked candidate List <Candidate> checkedCandidatesList = candidateData.Candidates.Where(x => x.IsChecked).ToList(); VotingDatabaseEntities db = new VotingDatabaseEntities(); //Encrypt the users personal id number LoggedVoter.PersonalIdNumber = Encrypter.EncryptString(LoggedVoter.PersonalIdNumber); //If single candidate was selected if (checkedCandidatesList.Count == 1) { //Add which candidate was selected LoggedVoter.CandidateId = checkedCandidatesList[0].CandidateId; } else { LoggedVoter.CandidateId = null; } //Update database db.Voters.Add(LoggedVoter); db.SaveChanges(); //Navigate user to the summary page SummaryPage summaryPage = new SummaryPage(); NavigationService.Navigate(summaryPage); } catch { MessageBox.Show("Could not save your vote! Try again.", "ERROR", MessageBoxButton.OK, MessageBoxImage.Error); } } }