Exemple #1
0
        private async void tryLogin()
        {
            // attempt connection
            using (SqlConnection conn = new SqlConnection(Configuration.CONNECTION_STRING))
            {
                try
                {
                    // try it
                    // tell the logger
                    logger.Info("Attempting connection with database");
                    logger.Info("Connection string: " + Configuration.CONNECTION_STRING);
                    await conn.OpenAsync();
                }
                catch (Exception ex)
                {
                    // it failed
                    // tell the user and the logger
                    conn.Close();
                    string errorMessage = "Error connecting to database: " + ex.Message;
                    MessageBox.Show(errorMessage, "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    logger.Error(ex, errorMessage);

                    button_OK.Enabled = true;

                    return;
                }

                switch (await authenticated(conn))
                {
                case AuthResult.SUCCESS:
                    // authentication successful
                    // tell the user and the logger
                    string authSuccessMessage = "Login successful";
                    MessageBox.Show(authSuccessMessage, "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    logger.Info(authSuccessMessage);

                    // config
                    Configuration.STAFF_ID = staffID;

                    // show main form and close this one
                    MainWindow mainForm = new MainWindow();
                    this.Hide();
                    conn.Close();
                    mainForm.ShowDialog();

                    logger.Info("Exiting application with Exit Code: " + Environment.ExitCode);
                    //Application.ExitThread();
                    //Environment.Exit(Environment.ExitCode);

                    break;

                case AuthResult.NO_USERNAME:
                    // no such username exists
                    // tell the user and the logger
                    string noUsernameMessage = "Username does not exist";
                    MessageBox.Show(noUsernameMessage, "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    logger.Info(noUsernameMessage);
                    conn.Close();

                    button_OK.Enabled = true;

                    break;

                case AuthResult.DENIED:
                    // authentication failed
                    // feedback for user
                    string authFailMessage = "Incorrect password";
                    MessageBox.Show(authFailMessage, "Retail POS", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    conn.Close();

                    button_OK.Enabled = true;

                    break;
                }
            }
        }