private void BtnLog_Click(object sender, EventArgs e) //Event handler when login button is clicked { if (checkBoxUser.Checked) //Checks to see if the remember username check box is checked, if so then saves the text from the username input text box to appropriate application properties variables { Properties.Settings.Default.userName = txtUser.Text; Properties.Settings.Default.checkboxRemember = true; Properties.Settings.Default.Save(); } else //Else clears the appropraite application properties variables { Properties.Settings.Default.userName = ""; Properties.Settings.Default.checkboxRemember = false; Properties.Settings.Default.Save(); } string ConnectionString = "Server=82.10.84.171;Database=prototypedatabase;Uid=Killian;Pwd=blackrock;"; //MySQL connection string string passHash; //holds hashed password string encryptedHash; //holds password after encrypting temporarily Global.currentUser = txtUser.Text; using (MySqlConnection con = new MySqlConnection(ConnectionString)) //Handles the execution of MySQL scoped commands { con.Open(); using (MySqlCommand cmd = con.CreateCommand()) { cmd.CommandText = "select HashSalt from userpass where UserName=@UserName"; //Input Query to database cmd.Parameters.Add(new MySqlParameter("UserName", txtUser.Text)); passHash = Convert.ToBase64String(Global.GetHashFunc(txtPass.Text, Convert.ToString(cmd.ExecuteScalar()))); //gets the resulting output of query, converts to string, the obtains the hash, again to a string of the equivalent base 64 digits } con.Close(); } encryptedHash = Global.EncryptString(passHash); //encrypts password hash using (MySqlConnection con = new MySqlConnection(ConnectionString)) //Again, scope for MySQL query commands { con.Open(); using (MySqlCommand cmd = con.CreateCommand()) { cmd.CommandText = @"select count(*) from userpass where UserName=@UserName and HashPass=@Password";//Input Query to database, will check the number of users with the correct username and encrypted password cmd.Parameters.Add(new MySqlParameter("UserName", txtUser.Text)); cmd.Parameters.Add(new MySqlParameter("Password", encryptedHash)); int i = Convert.ToInt32(cmd.ExecuteScalar()); //Gets resultant output of query if (i > 0) //If there does exists a correct username to password match { cmd.CommandText = @"select AccountType from prototypedatabase.userpass where username=@UserName"; //Input Query to database string accountType = cmd.ExecuteScalar().ToString(); //Gets resultant output of query if (accountType == "teacher") //Figures whether the account is a teacher or student account and opens appropriate form { TeacherFormIndex teacherFormIndex = new TeacherFormIndex(); teacherFormIndex.Show(); this.Close(); } else { StudentFormIndex studentFormIndex = new StudentFormIndex(); studentFormIndex.Show(); this.Close(); } } else //Presents user with error message { MessageBox.Show("UserName or Password Doesn't Exist."); } } } }
private void BtnNext_Click(object sender, EventArgs e) //Event handler for when the next button is clicked { switch (formType) //Switch case statement for the initialised quiz type { case 0: //If it is a Student quiz type btnBack.Text = "Back"; if (currentQuestion == listData.Count - 2) //If the current question is the last, then it submits the score { if (selectedAnswer == listData[currentQuestion][6]) { score++; } using (SubmitConfirmForm submit = new SubmitConfirmForm()) { if (submit.ShowDialog() == DialogResult.OK) { string teacherAccount = listData[listData.Count - 1][3]; //Gets required data from the list of string arrays string className = listData[listData.Count - 1][4]; string csvPath = Path.GetTempPath() + "tempcsv.csv"; FtpClient clientFTP = new FtpClient(); clientFTP.Host = "ftp://82.10.84.171"; clientFTP.Port = 54443; clientFTP.Credentials.UserName = "******"; clientFTP.Credentials.Password = "******"; clientFTP.DataConnectionReadTimeout = 2147483645; clientFTP.ConnectTimeout = 2147483645; clientFTP.DataConnectionConnectTimeout = 2147483645; clientFTP.ReadTimeout = 2147483645; clientFTP.DownloadFile(csvPath, $@"/Classes/{teacherAccount}/{className}/{Global.currentUser}.csv"); //obtains the current state of the students particular csv using (StreamWriter csvData = File.AppendText(csvPath)) { csvData.WriteLine($"\n{listData[0][0]},{score}"); //Writes the quiz title and resultant score } clientFTP.DeleteFile($@"/Students/{Global.currentUser}/{listData[0][0]}.csv"); //Delete appropriate files clientFTP.DeleteFile($@"/Classes/{teacherAccount}/{className}/{Global.currentUser}.csv"); clientFTP.UploadFile(csvPath, $@"/Classes/{teacherAccount}/{className}/{Global.currentUser}.csv"); //Upload edited file File.Delete(csvPath); MessageBox.Show("Successfully Submitted"); //Inform user the score was successfully submitted StudentFormIndex studentFormIndex = new StudentFormIndex(); //Go back to student index form and close this form studentFormIndex.Show(); this.Close(); } } } else if (currentQuestion == listData.Count - 3) //If the current question is the second to last { btn1.Enabled = true; //Perform the usual btn2.Enabled = true; btn3.Enabled = true; btn4.Enabled = true; if (selectedAnswer == listData[currentQuestion][6]) { score++; } btnNext.Text = "Submit"; //Simply rename the next button text Submit currentQuestion++; UpdateStudent(); } else { btn1.Enabled = true; //Re-enable all buttons btn2.Enabled = true; btn3.Enabled = true; btn4.Enabled = true; if (selectedAnswer == listData[currentQuestion][6]) //Check if the answer was correct, if so then increment the score counter { score++; } currentQuestion++; //increment current question pointer UpdateStudent(); } break; case 1: //If it is a View quiz type currentQuestion++; btnBack.Text = "Back"; if (currentQuestion == listData.Count - 1) //If current question is on the last question { currentQuestion = 1; //Loop back to the first btnBack.Text = "Close"; } UpdateView(); break; case 2: //If it is a Creation quiz type btnBack.Text = "Back"; if (currentQuestion == 19) { btnNext.Hide(); //When on last question, hide the next button so only back and submit are options for the user CreateNext(); currentQuestion++; DrawNextCreate(); } else if (currentQuestion == 2) { btnSubmit.Show(); //When past the 2nd question, enable user to submit whenever they want CreateNext(); currentQuestion++; DrawNextCreate(); } else { CreateNext(); //Applie creating next logic currentQuestion++; //Increment current question pointer DrawNextCreate(); //Draw a fresh question on form } break; } }