/// <summary> /// Service method to create new member /// </summary> /// <param name="clubMember">club member model</param> /// <returns>true or false</returns> public bool Create(ClubMember clubMember) { using (var context = new SocialClubDbContext()) { context.ClubMembers.Add(clubMember); return context.SaveChanges() > 0; } }
/// <summary> /// Click event to handle registration /// </summary> /// <param name="sender">sender object</param> /// <param name="e">event data</param> private void Register_Click(object sender, EventArgs e) { try { // Check if the validation passes if (this.ValidateRegistration()) { // Assign the values to the model ClubMember clubMemberModel = new ClubMember() { Id = 0, Name = txtName.Text.Trim(), DateOfBirth = dtDateOfBirth.Value, Occupation = (int)cmbOccupation.SelectedValue, HealthStatus = (int)cmbHealthStatus.SelectedValue, MaritalStatus = (int)cmbMaritalStatus.SelectedValue, Salary = txtSalary.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txtSalary.Text), NumberOfChildren = txtNoOfChildren.Text.Trim() == string.Empty ? 0 : Convert.ToInt16(txtNoOfChildren.Text) }; // Call the service method and assign the return status to variable var success = this.clubMemberService.Create(clubMemberModel); // if status of success variable is true then display a information else display the error message if (success) { // display the message box MessageBox.Show( Resources.Registration_Successful_Message, Resources.Registration_Successful_Message_Title, MessageBoxButtons.OK, MessageBoxIcon.Information); // Reset the screen this.ResetRegistration(); } else { // display the error messge MessageBox.Show( Resources.Registration_Error_Message, Resources.Registration_Error_Message_Title, MessageBoxButtons.OK, MessageBoxIcon.Error); } } else { // Display the validation failed message MessageBox.Show( this.errorMessage, Resources.Registration_Error_Message_Title, MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { this.ShowErrorMessage(ex); } }
/// <summary> /// Service method to update club member /// </summary> /// <param name="clubMember">club member</param> /// <returns>true / false</returns> public bool Update(ClubMember clubMember) { using (var context = new SocialClubDbContext()) { context.ClubMembers.Attach(clubMember); context.Entry(clubMember).State = EntityState.Modified; return context.SaveChanges() > 0; } }
/// <summary> /// Click event to update the data /// </summary> /// <param name="sender">sender object</param> /// <param name="e">event args</param> private void btnUpdate_Click(object sender, EventArgs e) { try { if (this.ValidateUpdate()) { ClubMember clubMemberModel = new ClubMember() { Id = this.memberId, Name = txt2Name.Text.Trim(), DateOfBirth = dt2DateOfBirth.Value, Occupation = (int)cmb2Occupation.SelectedValue, HealthStatus = (int)cmb2HealthStatus.SelectedValue, MaritalStatus = (int)cmb2MaritalStatus.SelectedValue, Salary = txt2Salary.Text.Trim() == string.Empty ? 0 : Convert.ToDecimal(txt2Salary.Text), NumberOfChildren = txt2NoOfChildren.Text.Trim() == string.Empty ? 0 : Convert.ToInt16(txt2NoOfChildren.Text) }; var flag = this.clubMemberService.Update(clubMemberModel); if (flag) { DataTable data = this.clubMemberService.GetAll(); this.LoadDataGridView(data); MessageBox.Show( Resources.Update_Successful_Message, Resources.Update_Successful_Message_Title, MessageBoxButtons.OK, MessageBoxIcon.Information); } } else { MessageBox.Show( this.errorMessage, Resources.Registration_Error_Message_Title, MessageBoxButtons.OK, MessageBoxIcon.Error); } } catch (Exception ex) { this.ShowErrorMessage(ex); } }