public static bool AddStudentToStudentDatabase(Student studentToAdd) { try { using ( StreamWriter writer = new StreamWriter(FilePath, true)) { writer.WriteLine(studentToAdd); } return true; } catch (Exception) { return false; } }
protected void btnAdd_Click(object sender, EventArgs e) { var student = new Student(this.txtName.Text, this.toggleActiveBox.SelectedValue, int.Parse(this.txtFacultyNumber.Text), double.Parse(this.txtAssessment.Text)); if (FileOperations.AddStudentToStudentDatabase(student)) { lblMessage.Text = "Student successfuly added to database"; System.Threading.Thread.Sleep(5000); } else { lblMessage.Text = "You f*****g idiot?!?"; } //Response.Redirect(Request.RawUrl); }
public static List<Student> ReadStudentsFromDatabase() { var listOfStudents = new List<Student>(); using (StreamReader reader = new StreamReader(FilePath)) { var currentLine = reader.ReadLine(); while (currentLine != null) { var currentStudent = currentLine.Split('|'); var student = new Student(currentStudent[0].Trim(), currentStudent[1].Trim(), int.Parse(currentStudent[2].Trim()), double.Parse(currentStudent[3].Trim())); listOfStudents.Add(student); currentLine = reader.ReadLine(); } } return listOfStudents; }