Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Configuration;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- using PADLibrary;
- using System.Threading;
- namespace MySQLTestProgram
- {
- public partial class LoginForm : Form
- {
- //Initialization
- public LoginForm()
- {
- InitializeComponent();
- usernameBox.Focus();
- }
- #region Functions to start new threads: Loading Screen and Location Prompt
- //Used for starting the program upon correct login information
- private static void LocationPrompt()
- {
- Application.Run(new LocationPrompt());
- }
- //Loading splash screen
- private static void LoadingProc()
- {
- Application.Run(new Loading());
- }
- #endregion
- #region Variables used in the current form
- PADLibrary.PADGeneration3 myPad = new PADGeneration3("COM3");
- HolderClass loginInfo = new HolderClass();
- private string username, password, server = null;
- #endregion
- #region Textbox keypress events, used for "Enter"
- private void usernameBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == 13)
- {
- e.Handled = true;
- if (passwordBox.Text == "")
- {
- passwordBox.Focus();
- }
- else if (serverBox.Text == "")
- {
- serverBox.Focus();
- }
- else
- {
- connectButton_Click(sender, e);
- }
- }
- }
- private void passwordBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == 13)
- {
- e.Handled = true;
- if (usernameBox.Text == "")
- {
- usernameBox.Focus();
- }
- else if (serverBox.Text == "")
- {
- serverBox.Focus();
- }
- else
- {
- connectButton_Click(sender, e);
- }
- }
- }
- private void serverBox_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar == 13)
- {
- e.Handled = true;
- if (usernameBox.Text == "")
- {
- usernameBox.Focus();
- }
- else if (passwordBox.Text == "")
- {
- passwordBox.Focus();
- }
- else
- {
- connectButton_Click(sender, e);
- }
- }
- }
- #endregion
- //Attemps to connect to the SQL server, and if it fails, throws an error and does not grant entry
- public void connectButton_Click(object sender, EventArgs e)
- {
- using (SqlConnection con = new SqlConnection()) //Will ensure that the connection is disposed of
- {
- //Sets the values from the user input
- username = usernameBox.Text;
- password = passwordBox.Text;
- server = serverBox.Text;
- con.ConnectionString = ("Data Source=" + server + ";Initial Catalog=Test;Integrated Security=True");
- //Initializes the HolderClass variables
- HolderClass.connection = con;
- HolderClass.conString = con.ConnectionString;
- HolderClass.username = username;
- HolderClass.password = password;
- HolderClass.connection = con;
- HolderClass.conString = con.ConnectionString;
- try //Attempts to open the connection
- {
- //Opens the connection
- con.Open();
- SqlCommand cmd = con.CreateCommand();
- cmd.CommandText = "SELECT Access FROM AccountPermissions WHERE Username = '" + username + "' AND Password = '" + password + "'";
- SqlDataReader reader = cmd.ExecuteReader();
- //Checks to see if the username/password provided are valid, and if they have proper account permissions
- if (reader.Read())
- {
- //Starts the loading screen
- Thread load = new Thread(new ThreadStart(LoadingProc));
- load.Start();
- //Gets the pad information and access level of the user
- ConfigurationManager.AppSettings.Set("access", reader[0].ToString());
- myPad.SerialPortOpen();
- myPad.GetPAD();
- myPad.GetPADUnitID();
- myPad.SerialPortClose();
- load.Abort();
- HolderClass.PadID = Int32.Parse(myPad.UnitID);
- //Starts the next sceen, the location prompt.
- System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(LocationPrompt));
- this.Close();
- t.Start();
- }
- else
- {
- //Cannot find a username/password combo that is in the database
- MessageBox.Show("Connection error. Please use a valid username or password for that server", "Incorrect login!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- usernameBox.Clear();
- passwordBox.Clear();
- usernameBox.Focus();
- }
- }
- catch //If the connection fails
- {
- MessageBox.Show("Connection error. Please use a valid username or password for that server", "Incorrect login!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- usernameBox.Clear();
- passwordBox.Clear();
- usernameBox.Focus();
- }
- }
- }
- //Quits the application
- private void exitButton_Click(object sender, EventArgs e)
- {
- Application.Exit();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement