Advertisement
EndymionSpr

Untitled

Jan 22nd, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using System.Data.SqlClient;
  11. using System.Configuration;
  12.  
  13. namespace LibraryManagementSystem
  14. {
  15.     public partial class login : Form
  16.     {
  17.         // variables
  18.         SqlConnection con;
  19.         SqlCommand cmd;
  20.  
  21.         // userid and pwd
  22.         public static int userid;
  23.         public static string password;
  24.  
  25.         private void login_Load(object sender, EventArgs e)
  26.         {
  27.             // initialise connection to db
  28.             string connectionString = ConfigurationManager.ConnectionStrings["LibraryManagementSystem.Properties.Settings.LibraryDB"].ToString();
  29.             con = new SqlConnection(connectionString);
  30.         }
  31.  
  32.         public login()
  33.         {
  34.             InitializeComponent();
  35.         }
  36.  
  37.         // functions to empty text boxes on login
  38.         public void clear()
  39.         {
  40.             loginTbxUserId.Text = string.Empty;
  41.             loginTbxPassword.Text = string.Empty;
  42.         }
  43.  
  44.         private void LoginBtnSubmit_Click(object sender, EventArgs e)
  45.         {
  46.             if (con.State == ConnectionState.Closed)
  47.                 con.Open();
  48.  
  49.             cmd = new SqlCommand("select * from users where user_id = @user_id and password = @password", con);
  50.             cmd.Parameters.AddWithValue("@user_id", loginTbxUserId.Text);
  51.             cmd.Parameters.AddWithValue("@password", loginTbxPassword.Text);
  52.  
  53.             SqlDataAdapter sda = new SqlDataAdapter(cmd);
  54.             DataTable dt = new DataTable();
  55.  
  56.             sda.Fill(dt);
  57.  
  58.             if (dt.Rows.Count > 0)
  59.             {
  60.                 // successfully logged in pass userid and pwd
  61.                 userid = Convert.ToInt32(loginTbxUserId.Text);
  62.                 password = loginTbxPassword.Text;
  63.  
  64.                 if (Convert.ToString(dt.Rows[0][1]) == "False")
  65.                 {
  66.                     this.Hide();
  67.                     userBookSearch ubs = new userBookSearch();
  68.                     ubs.Show();
  69.                 }
  70.                 else
  71.                 {
  72.                     this.Hide();
  73.                     admStartPage asp = new admStartPage();
  74.                     asp.Show();
  75.                 }
  76.             }
  77.             else
  78.             {
  79.                 MessageBox.Show("The entered USER ID or PASSWORD is WRONG.\nPlease check and try again.\nIf you have forgotten the password then go to the librarian to create a new one.");
  80.                 clear();
  81.             }
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement