//// When View-Lastname is clicked, the data appears sorted by lastname //private void mnuViewLastName_Click(object sender, EventArgs e) //{ // clsSortLName comparer = new clsSortLName(); // mSchool.Sort(comparer); // displayData(); //} //// When View-Firstname is clicked, the data appears sorted by firstname //private void mnuViewFirstName_Click(object sender, EventArgs e) //{ // clsSortFName comparer = new clsSortFName(); // mSchool.Sort(comparer); // displayData(); //} //// When View-Title is clicked, the data appears sorted by starting weight //private void mnuViewTitle_Click_1(object sender, EventArgs e) //{ // clsSortTitle comparer = new clsSortTitle(); // mSchool.Sort(comparer); // displayData(); //} //// When View-Email is clicked, the data appears sorted by goal weight //private void mnuViewEmail_Click(object sender, EventArgs e) //{ // clsSortEmail comparer = new clsSortEmail(); // mSchool.Sort(comparer); // displayData(); //} //// When View-Weeks is clicked, the data appears sorted by weeks to train //private void mnuViewPhone_Click(object sender, EventArgs e) //{ // clsSortPhone comparer = new clsSortPhone(); // mSchool.Sort(comparer); // displayData(); //} // Revise the selected items in the two combo boxes when a name is selected. private void lstStaff_SelectedIndexChanged(object sender, EventArgs e) { int selectedSchool = lstSchool.SelectedIndex - 2; // Make sure a valid client was selected. if (selectedSchool < 0 || selectedSchool >= mSchool.Count) { ShowMessage("Please select a school in the listbox."); lstSchool.SelectedIndex = -1; return; } // Get the ID of the selected client to fill in the input boxes clsSchool temp = (clsSchool)mSchool[selectedSchool]; txtSchool.Text = temp.SchoolName; txtAddress.Text = temp.Address; txtCity.Text = temp.City; cboState.Text = temp.State; txtContactName.Text = temp.ContactName; txtPhone.Text = temp.Phone; // Notify the user of the possible actions ShowMessage("To delete " + temp.SchoolName + " as a school, press the delete key. \n" + "To modify " + temp.SchoolName + "'s data, edit the data in the input fields and click Update."); }
// The loadSchool helper method reads the data from the specified file and copies // to the staff array. private void loadSchool(string sql) { clsSchool member; // Clear out the array before handling the file data. mSchool.Clear(); // Read the data from the specified file. if (File.Exists(mSchoolFile) == false) { ShowMessage(mSchoolFile + " does not exist. Please open another DB file."); return; } openDatabaseConnection(); mDB.Open(); OleDbCommand cmd; OleDbDataReader rdr; try { cmd = new OleDbCommand(sql, mDB); rdr = cmd.ExecuteReader(); while (rdr.Read() == true) { // Add the data from the line just read to the next array element, making sure to get the ID. member = new clsSchool((int)rdr["SchoolID"], (string)rdr["SchoolName"], (string)rdr["Address"], (string)rdr["City"], (string)rdr["State"], (string)rdr["ContactName"], (string)rdr["Phone"]); mSchool.Add(member); } rdr.Close(); } catch (Exception ex) { ShowMessage("There was an unexpected problem: " + ex.Message); } finally { closeDatabaseConnection(); } }
// This method removes the selected school from the roster private void btnDelete_Click(object sender, EventArgs e) { string sql; int selectedSchool = lstSchool.SelectedIndex - 2; // Make sure a valid school was selected. if (selectedSchool < 0 || selectedSchool >= mSchool.Count) { ShowMessage("Please select a valid school in the listbox."); lstSchool.SelectedIndex = -1; return; } // Get the ID of the selected client to fill in the input boxes clsSchool temp = (clsSchool)mSchool[selectedSchool]; // Delete the selected client and remove the corresponding record in the ClientPlan table try { openDatabaseConnection(); mDB.Open(); OleDbCommand cmd; sql = "DELETE FROM School WHERE SchoolID = " + clsSQL.ToSql(temp.SchoolID); cmd = new OleDbCommand(sql, mDB); cmd.ExecuteNonQuery(); } catch (Exception ex) { ShowMessage("There was an unexpected problem when deleting the school: " + ex.Message); } finally { closeDatabaseConnection(); } // Erase the input values, notify the user, and display the current school roster eraseInputFields(); ShowMessage(temp.SchoolName + "'s record has been deleted."); loadSchool("SELECT * FROM School"); displayData(); }
// When the Update button is clicked, the user is able to modify the data // for the selected staff member. private void btnUpdate_Click(object sender, EventArgs e) { // Define all variables. string sql; int selectedSchool = lstSchool.SelectedIndex - 2; // Make sure a valid client was selected. if (selectedSchool < 0 || selectedSchool >= mSchool.Count) { ShowMessage("Please select a valid staff member in the listbox."); lstSchool.SelectedIndex = -1; return; } // Get the ID of the selected client to fill in the input boxes clsSchool temp = (clsSchool)mSchool[selectedSchool]; // Validate the user's input. if (txtSchool.Text == "") { ShowMessage("Please enter the school name."); txtSchool.Focus(); return; } if (txtAddress.Text == "") { ShowMessage("Please enter the address."); txtAddress.Focus(); return; } if (txtCity.Text == "") { ShowMessage("Please enter the city."); txtAddress.Focus(); return; } if (txtContactName.Text == "") { ShowMessage("Please enter the contact name."); txtAddress.Focus(); return; } if (txtPhone.Text == "") { ShowMessage("Please enter phone number."); txtAddress.Focus(); return; } // Data is valid so modify the school and reload the data from the DB try { // Change a record in the School table. openDatabaseConnection(); mDB.Open(); OleDbCommand cmd; sql = "UPDATE School SET SchoolName = " + clsSQL.ToSql(txtSchool.Text) + ", Address = " + clsSQL.ToSql(txtAddress.Text) + ", City = " + clsSQL.ToSql(txtCity.Text) + ", State = " + clsSQL.ToSql(cboState.Text) + ", ContactName = " + clsSQL.ToSql(txtContactName.Text) + ", Phone = " + clsSQL.ToSql(txtPhone.Text) + " WHERE SchoolID = " + clsSQL.ToSql(temp.SchoolID); cmd = new OleDbCommand(sql, mDB); cmd.ExecuteNonQuery(); } catch (Exception ex) { ShowMessage("There was an unexpected problem: " + ex.Message); } finally { closeDatabaseConnection(); } // Erase the input values, notify the user, and display the current client roster eraseInputFields(); ShowMessage(temp.SchoolName + "'s record has been updated."); loadSchool("SELECT * FROM School"); displayData(); }