Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. // Event handler for button click
  2.  
  3.  
  4. private void button1_Click(object sender, EventArgs e)
  5.  
  6. {
  7.  
  8.  
  9.  
  10. // Check if the text boxes have any text
  11.  
  12.  
  13. if(string.IsNullOrEmpty(textBox1.Text))
  14.  
  15. { MessageBox.Show("You have to complete the username field!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  16.  
  17. textBox1.Focus();
  18.  
  19. }
  20.  
  21. else
  22. if(string.IsNullOrEmpty(textBox2.Text))
  23.  
  24. {
  25.  
  26. MessageBox.Show("You have to complete the password field!", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Stop);
  27.  
  28. textBox2.Focus();
  29.  
  30. }
  31.  
  32.  
  33. string connectionString = "Server = 127.0.0.1; Port = 3306; Database = db_admin; Uid = root;Pwd=;";
  34.  
  35. // Details for connection
  36.  
  37.  
  38. //string connectionString = "Server = 192.168.1.113;
  39.  
  40. // Port = 3306; Database = db_A030;
  41.  
  42. // Uid = user_A030;
  43.  
  44. // Pwd = pass_A030";
  45.  
  46.  
  47. string querry = "SELECT userType FROM users WHERE userName = '" + textBox1.Text + "' AND userPass = '" + textBox2.Text +"'";
  48.  
  49. // The querry for the database
  50.  
  51.  
  52. connection = new MySqlConnection(connectionString);
  53.  
  54. // Create a connection with the database
  55.  
  56.  
  57. command = new MySqlCommand(querry, connection);
  58.  
  59. // Create a command for the database
  60.  
  61.  
  62.  
  63. connection.Open();
  64.  
  65. // Connect to the database
  66.  
  67.  
  68. string userType = command.ExecuteScalar() as string;
  69.  
  70. // Execute the command and return the result as a string
  71.  
  72.  
  73. connection.Close();
  74.  
  75. // Close the connection to the database
  76.  
  77.  
  78.  
  79. // Check if any results have been returned.
  80.  
  81. // If there is no result, return an error to the user
  82.  
  83.  
  84. if (string.IsNullOrEmpty(userType))
  85.  
  86. {
  87.  
  88. MessageBox.Show("Wrong username or password!", "Error!", MessageBoxButtons.OK,MessageBoxIcon.Error);
  89.  
  90. textBox1.Text = "";
  91.  
  92. textBox2.Text = "";
  93.  
  94. }
  95.  
  96.  
  97. // If there is a result, check if the user is an administrator or an user based on what the // database returned and
  98.  
  99. // open the form for the administrator or the user
  100.  
  101.  
  102. else
  103. if (userType.Equals("A"))
  104.  
  105. {
  106.  
  107. AdminForm f = new AdminForm();
  108.  
  109. f.Show();
  110.  
  111. this.Close();
  112.  
  113. }
  114.  
  115. else
  116. if (userType.Equals("U"))
  117.  
  118. {
  119.  
  120. UserForm f = new UserForm();
  121.  
  122. f.Show();
  123.  
  124. this.Close();
  125.  
  126. }
  127.  
  128.  
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement