private void Highscores_Load(object sender, EventArgs e) { try { lblPlaces.Text = ""; lblUsernames.Text = ""; lblScores.Text = ""; //Create Scores objects for (int j = 0; j < Scores.Length; j++) { //Create object Scores[j] = new Scores(); } int i = 0; //Establish database connection db_connection(); //Get all of the selected question attributes SqlCommand cmd = new SqlCommand("SELECT * FROM HIGHSCORES ORDER BY Score ASC;", connect); //Read values SqlDataReader sdr = cmd.ExecuteReader(); //Whilst reading values, make local varable values to those from columns in selected row while (sdr.Read()) { Scores[i].ScoreID = Int32.Parse(sdr[0].ToString()); Scores[i].Username = sdr[1].ToString(); Scores[i].Score = Int32.Parse(sdr[2].ToString()); i++; } //close connection connect.Close(); //List for displaying values var places = new List <string>(); var usernames = new List <string>(); var scores = new List <string>(); //Add values from scores objects to lists for (i = 0; i < 10; i++) { places.Add((i + 1).ToString()); usernames.Add(Scores[i].Username); scores.Add((Scores[i].Score).ToString()); } //Display lists in labels lblPlaces.Text += places.Aggregate((x, y) => x + "\n" + y); lblUsernames.Text += usernames.Aggregate((x, y) => x + "\n" + y); lblScores.Text += scores.Aggregate((x, y) => x + "\n" + y); } catch { //Inform user there was an issue connecting to the database SqlCommand cmd = new SqlCommand("EXECUTE InvalidStoredProcedure", connect); MessageBox.Show("Issue Connecting to DataBase"); } }
public void EndGameWin() { try { //Create Scores objects for (int j = 0; j < Scores.Length; j++) { Scores[j] = new Scores(); } int i = 0; db_connection(); //Get all of the selected question attributes SqlCommand cmd = new SqlCommand("SELECT * FROM HIGHSCORES ORDER BY Score ASC;", connect); //Read values SqlDataReader sdr = cmd.ExecuteReader(); //Whilst reading values, make local varable values to those from columns in selected row while (sdr.Read()) { Scores[i].ScoreID = Int32.Parse(sdr[0].ToString()); Scores[i].Username = sdr[1].ToString(); Scores[i].Score = Int32.Parse(sdr[2].ToString()); i++; } //close connection connect.Close(); //If current user score is greater than current lowest score in highscores if (turnCounter < Scores[9].Score) { try { //Establish connection db_connection(); //Insert(Update) username and turncounter/score into highscore table cmd = new SqlCommand("With NewScores as ( select username, score from HIGHSCORES where ScoreID = @ScoreID ) update NewScores set username= @Username, score=@Score;", connect); cmd.Parameters.AddWithValue("@Username", currentUserGame.Username); cmd.Parameters.AddWithValue("@Score", turnCounter); cmd.Parameters.AddWithValue("@ScoreID", Scores[9].ScoreID); cmd.ExecuteNonQuery(); //Close connection connect.Close(); } catch (SqlException) { MessageBox.Show("Error Writing new Score to DataBase - Apologies"); } } //If not then tell them they didnt make it :( else { MessageBox.Show("You didn't make the Highscores. Better luck next time!"); } //Switch to high score form this.Hide(); var HighscoresWindow = new Highscores(); HighscoresWindow.currentUserHighscores = currentUserGame; HighscoresWindow.Closed += (s, args) => this.Close(); HighscoresWindow.Show(); } catch (SqlException) { MessageBox.Show("Error Retrieving Scores from DataBase - Apologies"); //Switch to high score form this.Hide(); var HighscoresWindow = new Highscores(); HighscoresWindow.currentUserHighscores = currentUserGame; HighscoresWindow.Closed += (s, args) => this.Close(); HighscoresWindow.Show(); } }