Beispiel #1
0
 /*
  *      Method added to allow the mass importing of database objects.  This function has specific formatting requirements
  *      for its data files, requiring them to be in Last Name, First Name, StudentID order for correct entry into the database.
  *      A note appears in a message box when clicked, but is still contingent on correct user input.
  */
 private void importDatabaseObjectsToolStripMenuItem_Click_1(object sender, EventArgs e)
 {
     //message to user to inform of input requirements
     MessageBox.Show("When importing students, the file must be in a comma separated format." + Environment.NewLine +
                     "Students must be listed LastName,FirstName,StudentID." + Environment.NewLine +
                     "Student IDs of fewer than 7 characters will have 0s appended to the beginning to meet length requirements.");
     try
     {
         using (OpenFileDialog readFile = new OpenFileDialog())
         {
             readFile.Filter           = "txt files (*.txt)|*.txt|csv files (*.csv)|*.csv|xml files (*.xml)|*.xml";
             readFile.FilterIndex      = 2;
             readFile.RestoreDirectory = true; //restore directory to default after files are selected
             int counter = 0;                  //counter to track how many students are successfully added
             if (readFile.ShowDialog() == DialogResult.OK)
             {
                 using (StreamReader inputFile = new StreamReader(readFile.FileName))
                 {
                     while (!inputFile.EndOfStream)                                                             //loop the entirety of the file
                     {
                         string[] inputData = inputFile.ReadLine().Split(',');                                  //split each line, hence the comma requirement
                         if (inputData[2].Length <= 7 && int.TryParse(inputData[2], out int pass))              //check to see if the user ID meets DB requirements
                         {
                             while (inputData[2].Length < 7)                                                    //the DB requires user IDs to be 7 digits, as Excel trims beginning 0s we must add them
                             {
                                 inputData[2] = "0" + inputData[2];                                             //append 0s to the ID until it is 7 digits long
                             }
                             Valid student = new Valid(inputData[1].Trim(), inputData[0].Trim(), inputData[2]); //create the student, encryption is handled in the Valid class functions
                             if (!student.DupeCheck(student.Pass))                                              //check to see if the user is already in the DB
                             {
                                 student.InsertStudent(student);                                                //insert new students
                                 counter++;                                                                     //increment counter to track number of students added
                             }
                             else
                             {
                                 continue;
                             }
                         }
                         else
                         {
                             //Message box is displayed if the third value on each comma separated line is not a number
                             MessageBox.Show("An error has occured.  Failed to find a number for Student ID" +
                                             " or Student ID was greater than seven digits long.");
                         }
                     }
                     MessageBox.Show(counter.ToString() + " students imported successfully.");   //displays the student counter to compare against
                                                                                                 //expected number of students added
                 }
             }
         }
     }
     catch
     {
         MessageBox.Show("An error has occured while attempting to import students." + Environment.NewLine +
                         "Import process has been terminated.");
     }
 }
Beispiel #2
0
 /*
  * This function handles inserting the students into the dbo.Students.  It performs validation
  * of name/passwords then calls a SQL function to check for duplicates in the StudentID DB field,
  * then calls the insert SQL function to add the student to the dbo.Students.
  */
 private void addButton_Click(object sender, EventArgs e)
 {
     try
     {
         if (firstBox.Text.Length > 0 && lastBox.Text.Length > 0)
         {
             /*
              * This section validates the name as password entry.  It creates boolean values
              * that are set to false if the text entered in either the first or last name box
              * contains a character other than a letter or apostrophe.  Current parameterization
              * in the SQL statements and forbidding the SQL comment character in name entry has so far
              * prevented injection and precluded the need for apostrophe escape sequencing
              */
             bool   validname = true;
             bool   validpass = false;
             string name      = firstBox.Text.Trim();
             foreach (char c in name)
             {
                 if (!char.IsLetter(c) && c != '\'') //verifies that the name is composed only of
                 {                                   //letters and apostrophe
                     validname = false;
                 }
             }
             if (!validname)
             {
                 MessageBox.Show("Please enter a valid first name.");
             }
             name = lastBox.Text.Trim();
             foreach (char c in name)
             {
                 if (!char.IsLetter(c) && c != '\'')
                 {
                     validname = false;
                 }
             }
             if (!validname)
             {
                 MessageBox.Show("Please enter a valid last name.");
             }
             if (passBox.Text.Length == 7)   //checks to ensure the password is exactly 7 characters
             {                               //can be adjusted based on current length of YT Student IDs
                 if (passBox.Text == confirmBox.Text)
                 {
                     string password = passBox.Text;
                     if (int.TryParse(password, out int temp))    //check password is a number
                     {
                         validpass = true;
                     }
                 }
             }
             if (!validpass)
             {
                 MessageBox.Show("Please enter your seven digit student ID");
                 passBox.Clear();
                 confirmBox.Clear();
                 passBox.Focus();
             }
             if (validname && validpass)
             {
                 Valid student = new Valid(firstBox.Text, lastBox.Text, passBox.Text);
                 //perform a check for duplicate Student IDs
                 if (student.DupeCheck(passBox.Text))
                 {
                     MessageBox.Show("Duplicate Student ID entry detected.");
                 }
                 else if (student.InsertStudent(student))    //attempts to insert new student
                 {
                     MessageBox.Show("Student added.");
                 }
                 else
                 {
                     MessageBox.Show("Failed to add student.");
                 }
                 //clear and reset boxes to allow for multiple student entry
                 //can be changed to a simple .Close statement if one entry per form load is preferred
                 firstBox.Clear();
                 lastBox.Clear();
                 passBox.Clear();
                 confirmBox.Clear();
                 firstBox.Focus();
             }
         }
     }
     catch
     {
         MessageBox.Show("Failed to add student.");
     }
 }