snippetcsharpModerate
Form to create new students or view existing student information
Viewed 0 times
studentnewcreatestudentsviewexistingforminformation
Problem
I started out with the best of intentions, but this form got hacky real fast.
It's purpose is to serve as a create new Student form. Also, if you want to view an existing Students information.
Think of it as the CRU of CRUD.
Here it is:
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tutomentor.Branding;
using Tutomentor.Data.Repositories;
namespace Tutomentor.UI.Students
{
public partial class StudentInformation : Form
{
StudentRepository repo = new StudentRepository();
bool IsCreating = false;
Student student;
public StudentInformation()
{
InitializeComponent();
LoadComboBoxes();
LoadBranding();
IsCreating = true;
}
public StudentInformation(int studentID)
{
InitializeComponent();
LoadComboBoxes();
LoadBranding();
student = repo.FindStudent(studentID);
LoadStudentInformation(student);
}
private void LoadComboBoxes()
{
cmbGrade.DisplayMember = "Name";
cmbGrade.ValueMember = "ID";
cmbGradeParalelo.DisplayMember = "Name";
cmbGradeParalelo.ValueMember = "ID";
GradeRepository repo = new GradeRepository();
cmbGrade.DataSource = repo.FindAllGrades();
}
private void LoadStudentInformation(Student student)
{
cmbGrade.SelectedValue = student.GradeParalelo.Grade.ID;
cmbGradeParalelo.SelectedValue = student.IDGrade;
txtRude.Text = student.RUDE.ToString();
txtNombrePadre.Text = student.FatherName;
txtProfesionPadre.Text = student.FatherProfession;
txtCelularPadre.Text = student.MobilePhoneFather;
txtLugarDeTrabajoPadre.Text = student.Placeo
It's purpose is to serve as a create new Student form. Also, if you want to view an existing Students information.
Think of it as the CRU of CRUD.
Here it is:
```
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tutomentor.Branding;
using Tutomentor.Data.Repositories;
namespace Tutomentor.UI.Students
{
public partial class StudentInformation : Form
{
StudentRepository repo = new StudentRepository();
bool IsCreating = false;
Student student;
public StudentInformation()
{
InitializeComponent();
LoadComboBoxes();
LoadBranding();
IsCreating = true;
}
public StudentInformation(int studentID)
{
InitializeComponent();
LoadComboBoxes();
LoadBranding();
student = repo.FindStudent(studentID);
LoadStudentInformation(student);
}
private void LoadComboBoxes()
{
cmbGrade.DisplayMember = "Name";
cmbGrade.ValueMember = "ID";
cmbGradeParalelo.DisplayMember = "Name";
cmbGradeParalelo.ValueMember = "ID";
GradeRepository repo = new GradeRepository();
cmbGrade.DataSource = repo.FindAllGrades();
}
private void LoadStudentInformation(Student student)
{
cmbGrade.SelectedValue = student.GradeParalelo.Grade.ID;
cmbGradeParalelo.SelectedValue = student.IDGrade;
txtRude.Text = student.RUDE.ToString();
txtNombrePadre.Text = student.FatherName;
txtProfesionPadre.Text = student.FatherProfession;
txtCelularPadre.Text = student.MobilePhoneFather;
txtLugarDeTrabajoPadre.Text = student.Placeo
Solution
You can simplify SaveInformation:
The second one is a style thing. Some people like this others hate it.
Additional note: It is unclear from the given text if p will have already been sanitized. As a result (and depending on expected usage) you want to also check for "m" (if this is a possibility).
private void SaveInformation()
{
if (IsCreating)
{
Student newStudent = new Student();
repo.Add(newStudent);
}
Int64 gradeId = Convert.ToInt64(cmbGradeParalelo.SelectedValue);
newStudent.IDGrade = gradeId;
newStudent.RUDE = Convert.ToInt64(txtRude.Text);
/*Parents information.*/
newStudent.FatherName = txtNombrePadre.Text;
newStudent.FatherProfession = txtProfesionPadre.Text;
newStudent.MobilePhoneFather = FormatPhoneNumber(txtCelularPadre.Text);
newStudent.PlaceofWorkFather = txtLugarDeTrabajoPadre.Text;
newStudent.MotherName = txtNombreMadre.Text;
newStudent.MotherProfession = txtProfesionMadre.Text;
newStudent.MobilePhoneMother = FormatPhoneNumber(txtCelularMadre.Text);
newStudent.PlaceofWorkMother = txtLugarDeTrabajoMadre.Text;
/*newStudent information*/
newStudent.Name = txtNombre.Text;
newStudent.FatherLastName = txtApellidoPaterno.Text;
newStudent.MotherLasteName = txtApellidoMaterno.Text;
newStudent.DateOfBirth = dtpFechaNacimiento.Value.ToShortDateString();
newStudent.PlaceOfBirth = txtLugarNacimiento.Text;
newStudent.Sex = sexoMasculino.Checked ? sexoMasculino.Text : sexoFemenino.Text;
newStudent.Telephone = FormatPhoneNumber(txtTelefono.Text);
newStudent.MobilePhone = FormatPhoneNumber(txtCelular.Text);
newStudent.Address = txtDireccion.Text;
newStudent.Carnet = FormatPhoneNumber(txtCarnet.Text);
newStudent.Observations = txtObservaciones.Text;
repo.Save();
MessageBox.Show("Se guardo el registro exitosamente.",
"Exito!",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
if (IsCreating)
{
ClearForm();
}
else
{
this.Close();
}
}The second one is a style thing. Some people like this others hate it.
private void SetSex(string p)
{
sexoMasculino.Checked = (p == "M");
sexoFemenino.Checked = !sexoMasculino.Checked;
}Additional note: It is unclear from the given text if p will have already been sanitized. As a result (and depending on expected usage) you want to also check for "m" (if this is a possibility).
Code Snippets
private void SaveInformation()
{
if (IsCreating)
{
Student newStudent = new Student();
repo.Add(newStudent);
}
Int64 gradeId = Convert.ToInt64(cmbGradeParalelo.SelectedValue);
newStudent.IDGrade = gradeId;
newStudent.RUDE = Convert.ToInt64(txtRude.Text);
/*Parents information.*/
newStudent.FatherName = txtNombrePadre.Text;
newStudent.FatherProfession = txtProfesionPadre.Text;
newStudent.MobilePhoneFather = FormatPhoneNumber(txtCelularPadre.Text);
newStudent.PlaceofWorkFather = txtLugarDeTrabajoPadre.Text;
newStudent.MotherName = txtNombreMadre.Text;
newStudent.MotherProfession = txtProfesionMadre.Text;
newStudent.MobilePhoneMother = FormatPhoneNumber(txtCelularMadre.Text);
newStudent.PlaceofWorkMother = txtLugarDeTrabajoMadre.Text;
/*newStudent information*/
newStudent.Name = txtNombre.Text;
newStudent.FatherLastName = txtApellidoPaterno.Text;
newStudent.MotherLasteName = txtApellidoMaterno.Text;
newStudent.DateOfBirth = dtpFechaNacimiento.Value.ToShortDateString();
newStudent.PlaceOfBirth = txtLugarNacimiento.Text;
newStudent.Sex = sexoMasculino.Checked ? sexoMasculino.Text : sexoFemenino.Text;
newStudent.Telephone = FormatPhoneNumber(txtTelefono.Text);
newStudent.MobilePhone = FormatPhoneNumber(txtCelular.Text);
newStudent.Address = txtDireccion.Text;
newStudent.Carnet = FormatPhoneNumber(txtCarnet.Text);
newStudent.Observations = txtObservaciones.Text;
repo.Save();
MessageBox.Show("Se guardo el registro exitosamente.",
"Exito!",
MessageBoxButtons.OK,
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1);
if (IsCreating)
{
ClearForm();
}
else
{
this.Close();
}
}private void SetSex(string p)
{
sexoMasculino.Checked = (p == "M");
sexoFemenino.Checked = !sexoMasculino.Checked;
}Context
StackExchange Code Review Q#343, answer score: 10
Revisions (0)
No revisions yet.