/// <summary> /// Delete a student from db. /// </summary> /// <param name="student"> /// A <b>Student</b> value. /// </param> /// <returns> /// A <b>bool</b> value indicates delete correct or not. /// </returns> public static bool DeleteStudent(Student student) { // Chack parameter if (student == null) { throw new ArgumentNullException(); } return DeleteStudent(student.Ids); }
/// <summary> /// Set add studnt tab page. /// </summary> /// <param name="sender"> /// A <b>object</b> value generated by IDE. /// </param> /// <param name="e"> /// A <b>EventArgs</b> value generated by IDE. /// </param> private void TabConContainer_SelectedIndexChanged(object sender, EventArgs e) { if (tcContainer.SelectedIndex == 0) { tcContainer.TabPages[1].Text = "Add Student"; ResetInformations(); _currStudent = null; this.btnUpdate.Visible = false; this.btnAdd.Visible = true; } }
/// <summary> /// Select students from the conditions. /// </summary> /// <param name="sender"> /// A <b>object</b> value generated by IDE. /// </param> /// <param name="e"> /// A <b>EventArgs</b> value generated by IDE. /// </param> private void ButtonUpdteSelectedStudent_Click(object sender, EventArgs e) { if (this.gvStudents.SelectedRows.Count < 1) { MessageBox.Show("Please select one row to update."); return; } Student student = (Student)this.gvStudents.SelectedRows[0].DataBoundItem; tcContainer.TabPages[1].Text = "Update student"; this.btnUpdate.Visible = true; this.btnAdd.Visible = false; this.txtStudentID.Text = student.StudentID.Trim(); this.txtFirstName.Text = student.FirstName.Trim(); this.txtLastName.Text = student.LastName.Trim(); this.txtPhone.Text = student.PhoneNum.Trim(); this.txtAge.Text = student.Age.ToString(); this.txtAddress.Text = student.Address.Trim(); this.txtFavorite.Text = student.Favorites.Trim(); if (student.Sex == Gender.Male) { this.rbFemale.Checked = false; } // this.cbMainSubject.SelectedText = student.MainSubject.Trim(); foreach (object obj in cbMainSubject.Items) { if (obj.ToString() == student.MainSubject.Trim()) { cbMainSubject.SelectedItem = obj; } } _currStudent = student; tcContainer.SelectedIndex = 1; }
/// <summary> /// Add a student. /// </summary> /// <param name="sender"> /// A <b>object</b> value generated by IDE. /// </param> /// <param name="e"> /// A <b>EventArgs</b> value generated by IDE. /// </param> private void ButtonAdd_Click(object sender, EventArgs e) { // Check student's Informations. if (!CheckStudentInfo()) { return; } Student student = new Student(this.txtStudentID.Text.Trim(), this.txtFirstName.Text.Trim(), this.txtLastName.Text.Trim(), Convert.ToInt32(this.txtAge.Text.Trim()), this.tbMale.Checked ? Gender.Male : Gender.Female, this.cbMainSubject.Text.Trim(), this.txtPhone.Text.Trim(), this.txtAddress.Text.Trim(), this.txtFavorite.Text.Trim()); if (student.InsertStudent()) { MessageBox.Show("Add student successfully!"); // Clear the informations. ResetInformations(); } }
public void UpdateStudentTest() { Student student = new Student(1, "2002", "xx", "xx", 26, Gender.Male, "xx", "xx", "xx", "xx"); bool actual = Utility.UpdateStudent(student); Assert.AreEqual(true, actual); }
public void UpdateStudentNotExistTest() { Student student = new Student(20000, "2002", "xx", "xx", 26, Gender.Male, "xx", "xx", "xx", "xx"); bool actual = Utility.UpdateStudent(student); Assert.AreEqual(false, actual); }
public void UpdateStudentLNametooLongTest() { Student student = new Student(20000, "2002222", "xxwwwww", "xxwwwwwwwwwweeeeeeeeeerrrrrrrrrrttttttttttddddddddddxy", 26, Gender.Male, "xx", "xx", "xx", "xx"); Utility.UpdateStudent(student); }
public void UpdateStudentIDtooLongTest() { Student student = new Student(20000, "200222222222222222222245555555555", "xx", "xx", 26, Gender.Male, "xx", "xx", "xx", "xx"); Utility.UpdateStudent(student); }
public void InsertStudentTest() { Student student = new Student("2001", "aa", "aa", 28, Gender.Female, "aa", "aa", "aa", "aa"); bool expected = true; bool actual; actual = Utility.InsertStudent(student); Assert.AreEqual(expected, actual); }
public void InsertStudentLNametooLongTest() { Student student = new Student("2001", "aaabbb", "aaeeeeeeeeeerrrrrrrrrrttttttttttyyyyyyyyyyddddddddddqw", 28, Gender.Female, "aa", "aa", "aa", "aa"); Utility.InsertStudent(student); }
public void InsertStudentIDtooLongTest() { Student student = new Student("2001kljjhhggffqwertyasdffgh", "aa", "aa", 28, Gender.Female, "aa", "aa", "aa", "aa"); Utility.InsertStudent(student); }
/// <summary> /// Update a student to db. /// </summary> /// <param name="student"> /// A <b>Student</b> value. /// </param> /// <returns> /// A <b>bool</b> value indicates update correct or not. /// </returns> public static bool UpdateStudent(Student student) { // Chack parameter if (student == null) { throw new ArgumentNullException(); } // Check parameters if (student.StudentID.Length > 20 || student.FirstName.Length > 50 || student.LastName.Length > 50) { // I think a better way is showing a message box. throw new ArgumentOutOfRangeException(); } // Update flag. bool isCorrect = false; // How many lines be updated. int updateLine = 0; SqlConnection conn = new SqlConnection(GetConnectionString()); SqlCommand comm = new SqlCommand(); try { comm.Connection = conn; // Using procedure. comm.CommandText = "Student_Update"; comm.CommandType = CommandType.StoredProcedure; // Add parameters for the command. comm.Parameters.Add(new SqlParameter("@StudentID", SqlDbType.Char, 20)); comm.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.Char, 50)); comm.Parameters.Add(new SqlParameter("@LastName", SqlDbType.Char, 50)); comm.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int, 4)); comm.Parameters.Add(new SqlParameter("@Sex", SqlDbType.Int, 4)); comm.Parameters.Add(new SqlParameter("@MainSubject", SqlDbType.Char, 100)); comm.Parameters.Add(new SqlParameter("@Phone", SqlDbType.Char, 20)); comm.Parameters.Add(new SqlParameter("@Address", SqlDbType.Char, 100)); comm.Parameters.Add(new SqlParameter("@Favorites", SqlDbType.Char, 200)); comm.Parameters.Add(new SqlParameter("@IDs", SqlDbType.Int, 4)); comm.Parameters[0].Value = student.StudentID; comm.Parameters[1].Value = student.FirstName; comm.Parameters[2].Value = student.LastName; comm.Parameters[3].Value = student.Age; comm.Parameters[4].Value = (Int32)student.Sex; comm.Parameters[5].Value = student.MainSubject; comm.Parameters[6].Value = student.PhoneNum; comm.Parameters[7].Value = student.Address; comm.Parameters[8].Value = student.Favorites; comm.Parameters[9].Value = student.Ids; conn.Open(); updateLine = comm.ExecuteNonQuery(); } catch (SqlException) { throw; } catch (Exception) { throw; } finally { conn.Close(); } // Set update flag. if (updateLine == 1) { isCorrect = true; } return isCorrect; }
/// <summary> /// Insert a student to db and return it's ids. /// </summary> /// <param name="student"> /// A <b>Student</b> value. /// </param> /// <returns> /// A <b>bool</b> value indicates insert correct or not. /// </returns> public static bool InsertStudent(Student student) { // Chack parameter if (student == null) { throw new ArgumentNullException(); } // Check parameters if (student.StudentID.Length > 20 || student.FirstName.Length > 50 || student.LastName.Length > 50) { // I think a better way is showing a message box. throw new ArgumentOutOfRangeException(); } // Keep insert status. bool insertCorrect = false; // How many lines be inserted to the db. int insertLine = 0; SqlConnection conn = new SqlConnection(GetConnectionString()); SqlCommand comm = new SqlCommand(); try { comm.Connection = conn; // Using procedure. comm.CommandText = "Student_Insert"; comm.CommandType = CommandType.StoredProcedure; // Add parameters for the command. comm.Parameters.Add(new SqlParameter("@StudentID", SqlDbType.Char, 20)); comm.Parameters.Add(new SqlParameter("@FirstName", SqlDbType.Char, 50)); comm.Parameters.Add(new SqlParameter("@LastName", SqlDbType.Char, 50)); comm.Parameters.Add(new SqlParameter("@Age", SqlDbType.Int, 4)); comm.Parameters.Add(new SqlParameter("@Sex", SqlDbType.Int, 4)); comm.Parameters.Add(new SqlParameter("@MainSubject", SqlDbType.Char, 100)); comm.Parameters.Add(new SqlParameter("@Phone", SqlDbType.Char, 20)); comm.Parameters.Add(new SqlParameter("@Address", SqlDbType.Char, 100)); comm.Parameters.Add(new SqlParameter("@Favorites", SqlDbType.Char, 200)); comm.Parameters.Add(new SqlParameter("@IDs", SqlDbType.Int, 4, ParameterDirection.Output, true, 0, 0, "IDs", DataRowVersion.Default, 0)); comm.Parameters[0].Value = student.StudentID; comm.Parameters[1].Value = student.FirstName; comm.Parameters[2].Value = student.LastName; comm.Parameters[3].Value = student.Age; comm.Parameters[4].Value = (Int32)student.Sex; comm.Parameters[5].Value = student.MainSubject; comm.Parameters[6].Value = student.PhoneNum; comm.Parameters[7].Value = student.Address; comm.Parameters[8].Value = student.Favorites; conn.Open(); insertLine = comm.ExecuteNonQuery(); } catch (SqlException) { throw; } catch (Exception) { throw; } finally { conn.Close(); } // Get the student's ids. int identityID = int.Parse(comm.Parameters[9].Value.ToString()); if (identityID > 0 && insertLine == 1) { student.Ids = identityID; insertCorrect = true; } return insertCorrect; }