p-GeoGame / CSharp_GeographyGame / usernameForm.cs
usernameForm.cs
Raw
using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
using System.Threading;

namespace CSharp_GeographyGame
{
    public partial class usernameForm : Form
    {

        string connectionString;
        SqlConnection connOne;

        public usernameForm()
        {
            InitializeComponent();
            connectionString = ConfigurationManager.ConnectionStrings["CSharp_GeographyGame.Properties.Settings.userScoresConnectionString"].ConnectionString;
        }

        public string getName
        {
            get { return tbNameInput.Text; }
            set { tbNameInput.Text = value; }
        }


        private void btnName_Click(object sender, EventArgs e)
        {
            string nameID = tbNameInput.Text;

            if (tbNameInput.Text == "")
            {
                MessageBox.Show("Read the one instruction, idiot.", "Login Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            else
            {
                FormProvider.userName = getName;
                FormProvider.gForm.Show();
                //gamemenuForm menuForm = new gamemenuForm(nameID);
                //menuForm.Show();
                this.Hide();
            }
        }

        private void viewDatabasesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            bool isOpen = false;

            foreach (Form F in Application.OpenForms)
            {
                if (F.Name == "dbscoresForm")
                {
                    isOpen = true;
                    F.BringToFront();
                }
            }
            if (isOpen == false)
            {
                dbscoresForm dbF = new dbscoresForm();
                dbF.Show();
            }
        }

        private void usernameForm_Shown(object sender, EventArgs e)
        {
            bgWorker.RunWorkerAsync();
        }

        private void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            if (bgWorker.CancellationPending)
            {
                e.Cancel = true;
                return;
            }
            bool connOneTest = true;
            bool connTwoTest = true;

            progBar.Invoke((MethodInvoker)(() => progBar.Minimum = 0));
            progBar.Invoke((MethodInvoker)(() => progBar.Maximum = 2));

            Thread.Sleep(2000);

            using (connOne = new SqlConnection(connectionString))
            using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM USMapscores", connOne))
            {
                try
                {
                    connOne.Open();
                    connOne.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Local failed because: " + ex.Message);
                    connOneTest = false;
                }

                if (connOneTest == false)
                {
                    progBar.Invoke((MethodInvoker)(() => progBar.Increment(1)));
                    lblLocalStatus.Invoke((MethodInvoker)(() => lblLocalStatus.Text = "Not Connected"));
                    pblocalNo.Invoke((MethodInvoker)(() => pblocalNo.Visible = true));
                    //MessageBox.Show("Error locating the local database. Please read the installation instructions and try this test again.", "Test One: Local DB Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    progBar.Invoke((MethodInvoker)(() => progBar.Increment(1)));
                    lblLocalStatus.Invoke((MethodInvoker)(() => lblLocalStatus.Text = "Connected"));
                    pblocalYes.Invoke((MethodInvoker)(() => pblocalYes.Visible = true));
                    //MessageBox.Show("Local database was found. You can submit your scores without any errors.", "Test One: Local DB Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

            }

            string conWeb = "Server = remotemysql.com; Port = 3306; database = 2Ds6YxqaDW; username = 2Ds6YxqaDW; password = VJKMThFkcO";
            MySqlConnection connTwo = new MySqlConnection(conWeb);
            using (MySqlDataAdapter adapter = new MySqlDataAdapter("SELECT * FROM onUSmapscores", conWeb))
            {
                try
                {
                    connTwo.Open();
                    connOne.Close();
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Online failed because: " + ex.Message);
                    connTwoTest = false;
                }

                if (connTwoTest == false)
                {
                    progBar.Invoke((MethodInvoker)(() => progBar.Increment(1)));
                    lblOnlineStatus.Invoke((MethodInvoker)(() => lblOnlineStatus.Text = "Not Connected"));
                    pbOnlineNo.Invoke((MethodInvoker)(() => pbOnlineNo.Visible = true));
                    //MessageBox.Show("Error locating the online database. Please contact fernandezjchris@gmail.com to fix the online database.", "Test One: Online DB Connection Failed", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    progBar.Invoke((MethodInvoker)(() => progBar.Increment(1)));
                    lblOnlineStatus.Invoke((MethodInvoker)(() => lblOnlineStatus.Text = "Connected"));
                    pbOnlineYes.Invoke((MethodInvoker)(() => pbOnlineYes.Visible = true));
                    //MessageBox.Show("Online database was found. You can submit your scores without any errors.", "Test Two: Online DB Found", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }

            if (progBar.Value == 2) // To end bgWorker and jump to work completed function
                return;
        }

        private void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progBar.Value = e.ProgressPercentage;
        }

        private void bgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            if (e.Cancelled)
            {
                MessageBox.Show("Error. I suck.");
            }
            else if (e.Error != null)
            {
                MessageBox.Show("Ran into an unknown error... I really suck.");
            }
            else
            {
                // Do nothing.
            }
        }

        private void usernameForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            bgWorker.CancelAsync();
        }

        private void usernameForm_Leave(object sender, EventArgs e)
        {
            bgWorker.CancelAsync();
        }
    }
}