Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.53 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. using PADLibrary;
  12. using System.Threading;
  13. namespace MySQLTestProgram
  14. {
  15.     public partial class LoginForm : Form
  16.     {
  17.         //Initialization
  18.         public LoginForm()
  19.         {
  20.             InitializeComponent();
  21.             usernameBox.Focus();
  22.         }
  23.  
  24.         #region Functions to start new threads: Loading Screen and Location Prompt
  25.         //Used for starting the program upon correct login information
  26.         private static void LocationPrompt()
  27.         {
  28.             Application.Run(new LocationPrompt());
  29.         }
  30.         //Loading splash screen
  31.         private static void LoadingProc()
  32.         {
  33.             Application.Run(new Loading());
  34.         }
  35.         #endregion
  36.  
  37.         #region Variables used in the current form
  38.         PADLibrary.PADGeneration3 myPad = new PADGeneration3("COM3");
  39.         HolderClass loginInfo = new HolderClass();
  40.         private string username, password, server = null;
  41.         #endregion
  42.  
  43.         #region Textbox keypress events, used for "Enter"
  44.         private void usernameBox_KeyPress(object sender, KeyPressEventArgs e)
  45.         {
  46.             if (e.KeyChar == 13)
  47.             {
  48.                 e.Handled = true;
  49.                 if (passwordBox.Text == "")
  50.                 {
  51.                     passwordBox.Focus();
  52.                 }
  53.                 else if (serverBox.Text == "")
  54.                 {
  55.                     serverBox.Focus();
  56.                 }
  57.                 else
  58.                 {
  59.                     connectButton_Click(sender, e);
  60.                 }
  61.             }
  62.         }
  63.         private void passwordBox_KeyPress(object sender, KeyPressEventArgs e)
  64.         {
  65.             if (e.KeyChar == 13)
  66.             {
  67.                 e.Handled = true;
  68.                 if (usernameBox.Text == "")
  69.                 {
  70.                     usernameBox.Focus();
  71.                 }
  72.                 else if (serverBox.Text == "")
  73.                 {
  74.                     serverBox.Focus();
  75.                 }
  76.                 else
  77.                 {
  78.                     connectButton_Click(sender, e);
  79.                 }
  80.             }
  81.         }
  82.         private void serverBox_KeyPress(object sender, KeyPressEventArgs e)
  83.         {
  84.             if (e.KeyChar == 13)
  85.             {
  86.                 e.Handled = true;
  87.                 if (usernameBox.Text == "")
  88.                 {
  89.                     usernameBox.Focus();
  90.                 }
  91.                 else if (passwordBox.Text == "")
  92.                 {
  93.                     passwordBox.Focus();
  94.                 }
  95.                 else
  96.                 {
  97.                     connectButton_Click(sender, e);
  98.                 }
  99.             }
  100.         }
  101.         #endregion
  102.  
  103.         //Attemps to connect to the SQL server, and if it fails, throws an error and does not grant entry
  104.         public void connectButton_Click(object sender, EventArgs e)
  105.         {
  106.             using (SqlConnection con = new SqlConnection()) //Will ensure that the connection is disposed of
  107.             {
  108.                 //Sets the values from the user input
  109.                 username = usernameBox.Text;
  110.                 password = passwordBox.Text;
  111.                 server = serverBox.Text;
  112.                 con.ConnectionString = ("Data Source=" + server + ";Initial Catalog=Test;Integrated Security=True");
  113.  
  114.                 //Initializes the HolderClass variables
  115.                 HolderClass.connection = con;
  116.                 HolderClass.conString = con.ConnectionString;
  117.                 HolderClass.username = username;
  118.                 HolderClass.password = password;
  119.                 HolderClass.connection = con;
  120.                 HolderClass.conString = con.ConnectionString;
  121.                
  122.                 try //Attempts to open the connection
  123.                 {
  124.                     //Opens the connection
  125.                     con.Open();
  126.  
  127.                     SqlCommand cmd = con.CreateCommand();
  128.                     cmd.CommandText = "SELECT Access FROM AccountPermissions WHERE Username = '" + username + "' AND Password = '" + password + "'";
  129.                     SqlDataReader reader = cmd.ExecuteReader();
  130.  
  131.                     //Checks to see if the username/password provided are valid, and if they have proper account permissions
  132.                     if (reader.Read())
  133.                     {
  134.                         //Starts the loading screen
  135.                         Thread load = new Thread(new ThreadStart(LoadingProc));
  136.                         load.Start();
  137.  
  138.                         //Gets the pad information and access level of the user
  139.                         ConfigurationManager.AppSettings.Set("access", reader[0].ToString());
  140.                         myPad.SerialPortOpen();
  141.                         myPad.GetPAD();
  142.                         myPad.GetPADUnitID();
  143.                         myPad.SerialPortClose();
  144.                         load.Abort();
  145.                         HolderClass.PadID = Int32.Parse(myPad.UnitID);
  146.  
  147.                         //Starts the next sceen, the location prompt.
  148.                         System.Threading.Thread t = new System.Threading.Thread(new System.Threading.ThreadStart(LocationPrompt));
  149.                         this.Close();
  150.                         t.Start();
  151.                     }
  152.                     else
  153.                     {
  154.                         //Cannot find a username/password combo that is in the database
  155.                         MessageBox.Show("Connection error. Please use a valid username or password for that server", "Incorrect login!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  156.                         usernameBox.Clear();
  157.                         passwordBox.Clear();
  158.                         usernameBox.Focus();
  159.                     }
  160.                 }
  161.                 catch //If the connection fails
  162.                 {
  163.                     MessageBox.Show("Connection error. Please use a valid username or password for that server", "Incorrect login!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  164.                     usernameBox.Clear();
  165.                     passwordBox.Clear();
  166.                     usernameBox.Focus();
  167.                 }
  168.             }
  169.         }
  170.  
  171.         //Quits the application
  172.         private void exitButton_Click(object sender, EventArgs e)
  173.         {
  174.             Application.Exit();
  175.         }
  176.  
  177.     }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement