Advertisement
Guest User

Untitled

a guest
Feb 20th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.Windows;
  7. using System.Windows.Controls;
  8. using System.Windows.Data;
  9. using System.Windows.Documents;
  10. using System.Windows.Input;
  11. using System.Windows.Media;
  12. using System.Windows.Media.Imaging;
  13. using System.Windows.Navigation;
  14. using System.Windows.Shapes;
  15. using MySql.Data.MySqlClient;
  16.  
  17. namespace mysql
  18. {
  19. /// <summary>
  20. /// Interaction logic for LoginPage.xaml
  21. /// </summary>
  22. public partial class LoginPage : Page
  23. {
  24. private string conn;
  25. private MySqlConnection connect;
  26.  
  27. AdminPage ap = new AdminPage();
  28.  
  29. public LoginPage()
  30. {
  31. InitializeComponent();
  32. }
  33.  
  34. private void db_connection()
  35. {
  36. try
  37. {
  38. conn = "Server=localhost;Database=student;Uid=root;Pwd=admin;";
  39. connect = new MySqlConnection(conn);
  40. connect.Open();
  41. }
  42. catch (MySqlException e)
  43. {
  44. throw;
  45. }
  46. }
  47.  
  48. private bool validate_login(string user, string pass)
  49. {
  50. db_connection();
  51. MySqlCommand cmd = new MySqlCommand();
  52. cmd.CommandText = "Select * from student.admins where username=@user and password=@pass";
  53. cmd.Parameters.AddWithValue("@user", user);
  54. cmd.Parameters.AddWithValue("@pass", pass);
  55. cmd.Connection = connect;
  56. MySqlDataReader login = cmd.ExecuteReader();
  57. if (login.Read())
  58. {
  59. connect.Close();
  60. return true;
  61. }
  62. else
  63. {
  64. connect.Close();
  65. return false;
  66. }
  67. }
  68.  
  69. private void btn_login_Click(object sender, RoutedEventArgs e)
  70. {
  71. string user = txt_admin_name.Text;
  72. string pass = txt_admin_passwd.Password;
  73. if (user == "" || pass == "")
  74. {
  75. //MessageBox.Show("Empty Fields Detected ! Please fill up all the fields");
  76. txt_errormessage.Text = "Empty Fields Detected! Please fill up all the fields!";
  77. return;
  78. }
  79. bool r = validate_login(user, pass);
  80. if (r)
  81. {
  82. //MessageBox.Show("Correct Login Credentials.n You will be taken the Admin Page!");
  83. this.NavigationService.Navigate(ap);
  84. }
  85. else
  86. //MessageBox.Show("Incorrect Login Credentials");
  87. txt_errormessage.Text = "Incorrect Login Credentials!";
  88. }
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement