Advertisement
Guest User

Untitled

a guest
May 29th, 2017
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 12.22 KB | None | 0 0
  1. //Welcome to the Descartes Login page
  2. //This form reads and writes to the database file called banking.mdb
  3. //It also writes to a .ini file storing a variable which will be called upon in other pages
  4.  
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.Data;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Windows.Forms;
  13. using System.Data.OleDb;
  14. using System.IO;
  15.  
  16. namespace Descartes
  17. {
  18.     public partial class frmLogin : Form
  19.     {
  20.  
  21.         public frmLogin()
  22.         {
  23.             InitializeComponent();
  24.         }
  25.         int Check = 0;
  26.  
  27.         /*OleDbConnection banking;
  28.         OleDbCommand accountCommand;
  29.         OleDbDataAdapter accountAdapter;
  30.         DataTable accountTable;
  31.         OleDbCommand itemsCommand;                   //ignore this, this is the old code which was
  32.         OleDbDataAdapter itemsAdapter;               //a totally different approach to the database
  33.         DataTable itemsTable;
  34.         OleDbCommand purchasesCommand;
  35.         OleDbDataAdapter purchasesAdapter;
  36.         DataTable purchasesTable;*/
  37.  
  38.         OleDbConnection con;
  39.         DataSet ds1;                          //these are the variables for the connection to the db
  40.         OleDbDataAdapter da;
  41.         int MaxRows = 0;
  42.         int inc = 0;
  43.         string CurrentUser;                  //these will be used to check if the user/pass was correct
  44.         string CurrentPassword;
  45.         string UserID;
  46.         bool Found = false;
  47.  
  48.         private void tmrFadeIn_Tick(object sender, EventArgs e)
  49.         {
  50.             this.Opacity += 0.02;           //fades the page in
  51.         }
  52.  
  53.         private void tmrWait_Tick(object sender, EventArgs e)
  54.         {
  55.            tmrFadeIn.Enabled = false;      //stops the fade in function to save memory
  56.         }
  57.  
  58.         private void btnExit_Click(object sender, EventArgs e)
  59.         {
  60.             this.Close();
  61.             Application.Exit();           //closes the application
  62.         }
  63.  
  64.         private void btnNew_Click(object sender, EventArgs e)
  65.         {
  66.             //Basically switches between the create new user interface and the existing login interface
  67.  
  68.             if (Check == 0)
  69.             {
  70.                 txtUser.Visible = false;
  71.                 txtNewUser.Visible = true;
  72.                 txtPass.Visible = false;
  73.                 txtNewPass.Visible = true;
  74.                 btnLogin.Visible = false;
  75.                 btnReset.Visible = false;
  76.                 btnCreateNew.Visible = true;
  77.                 btnNew.Text = "Back to log in";
  78.                 txtUser.Text = "Username";
  79.                 txtNewUser.Text = "New username";
  80.                 txtPass.Text = "Password";
  81.                 txtNewPass.Text = "New password";
  82.                 Check = 1;
  83.  
  84.             }
  85.  
  86.             else if (Check == 1)
  87.             {
  88.  
  89.                 txtUser.Visible = true;
  90.                 txtNewUser.Visible = false;
  91.                 txtPass.Visible = true;
  92.                 txtNewPass.Visible = false;
  93.                 btnLogin.Visible = true;
  94.                 btnReset.Visible = true;
  95.                 btnCreateNew.Visible = false;
  96.                 btnNew.Text = "Create a new account";
  97.                 txtUser.Text = "Username";
  98.                 txtNewUser.Text = "New username";
  99.                 txtPass.Text = "Password";
  100.                 txtNewPass.Text = "New password";
  101.                 Check = 0;
  102.             }
  103.         }
  104.  
  105.         private void btnCreateNew_Click(object sender, EventArgs e)
  106.         {
  107.             //have yet to do this code, could be done but we will see if i have time
  108.         }
  109.  
  110.         private void txtNewPass_Enter(object sender, EventArgs e)
  111.         {
  112.             //When the new password box is selected, it will add a password mask to the box
  113.             //Also, if the new username box is empty, it will reset the box to say New Username
  114.  
  115.             txtNewPass.PasswordChar = '*';
  116.  
  117.             if (txtNewPass.Text == "New Password")
  118.             {
  119.                 txtNewPass.Text = "";
  120.                 txtNewPass.ForeColor = Color.Black;
  121.             }
  122.  
  123.             if (txtNewUser.Text == "")
  124.             {
  125.                 txtNewUser.Text = "New Username";
  126.                 txtNewUser.ForeColor = Color.Silver;
  127.             }
  128.         }
  129.  
  130.         private void txtPass_Enter(object sender, EventArgs e)
  131.         {
  132.             //When the  password box is selected, it will add a password mask to the box
  133.             //Also, if the  username box is empty, it will reset the box to say Username
  134.  
  135.             txtPass.PasswordChar = '*';
  136.  
  137.             if (txtPass.Text == "Password")
  138.             {
  139.                 txtPass.Text = "";
  140.                 txtPass.ForeColor = Color.Black;
  141.             }
  142.  
  143.             if (txtUser.Text == "")
  144.             {
  145.                 txtUser.Text = "Username";
  146.                 txtUser.ForeColor = Color.Silver;
  147.             }
  148.         }
  149.  
  150.         private void txtUser_Enter(object sender, EventArgs e)
  151.         {
  152.             //If the  password box is empty, it will reset the box to say Password
  153.  
  154.             if (txtUser.Text == "Username")
  155.             {
  156.                 txtUser.Text = "";
  157.                 txtUser.ForeColor = Color.Black;
  158.             }
  159.  
  160.             if (txtPass.Text == "")
  161.             {
  162.                 txtPass.PasswordChar = '\0';
  163.                 txtPass.Text = "Password";
  164.                 txtPass.ForeColor = Color.Silver;
  165.             }
  166.         }
  167.  
  168.         private void txtNewUser_Enter(object sender, EventArgs e)
  169.         {
  170.             //If the new password box is empty, it will reset the box to say New Password
  171.  
  172.             if (txtNewUser.Text == "New Username")
  173.             {
  174.                 txtNewUser.Text = "";
  175.                 txtNewUser.ForeColor = Color.Black;
  176.             }
  177.  
  178.             if (txtNewPass.Text == "")
  179.             {
  180.                 txtNewPass.PasswordChar = '\0';
  181.                 txtNewPass.Text = "New Password";
  182.                 txtNewPass.ForeColor = Color.Silver;
  183.             }
  184.         }
  185.  
  186.         private void frmLogin_Load(object sender, EventArgs e)
  187.         {
  188.             //This coding was aided by the PizzaDB program
  189.  
  190.            /* banking = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " + Application.StartupPath + "\\..\\..\\banking.mdb");
  191.             banking.Open();
  192.  
  193.             accountCommand = new OleDbCommand("SELECT * FROM account ORDER BY UserID", banking);
  194.             accountAdapter = new OleDbDataAdapter();
  195.             accountAdapter.SelectCommand = accountCommand;
  196.             accountTable = new DataTable();
  197.             accountAdapter.Fill(accountTable);
  198.  
  199.             itemsCommand = new OleDbCommand("SELECT * FROM items ORDER BY ItemID", banking);
  200.             itemsAdapter = new OleDbDataAdapter();
  201.             itemsAdapter.SelectCommand = itemsCommand;
  202.             itemsTable = new DataTable();
  203.             itemsAdapter.Fill(itemsTable);
  204.  
  205.             purchasesCommand = new OleDbCommand("SELECT * FROM purchases ORDER BY PurchaseID", banking);
  206.             purchasesAdapter = new OleDbDataAdapter();
  207.             purchasesAdapter.SelectCommand = purchasesCommand;
  208.             purchasesTable = new DataTable();
  209.             purchasesAdapter.Fill(purchasesTable);*/
  210.  
  211.             //ignore the above code, none of this was needed
  212.  
  213.         }
  214.  
  215.         private void btnReset_Click(object sender, EventArgs e)
  216.         {
  217.             //Resets the textboxes to their original values
  218.  
  219.             txtUser.Text = "Username";
  220.             txtUser.ForeColor = Color.Silver;
  221.             txtNewUser.Text = "New Username";
  222.             txtNewUser.ForeColor = Color.Silver;
  223.             txtPass.Text = "Password";
  224.             txtPass.ForeColor = Color.Silver;
  225.             txtNewPass.Text = "New Password";
  226.             txtNewPass.ForeColor = Color.Silver;
  227.             txtPass.PasswordChar = '\0';
  228.             txtNewPass.PasswordChar = '\0';
  229.         }
  230.  
  231.         private void btnLogin_Click(object sender, EventArgs e)
  232.         {
  233.             con = new OleDbConnection();
  234.             ds1 = new DataSet();
  235.             //this is the data connection
  236.            
  237.             con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " + Application.StartupPath + "\\..\\..\\banking.mdb"; //connection to database
  238.             con.Open();
  239.  
  240.             string sql = "SELECT * From account"; //select everything from the account table
  241.             da = new OleDbDataAdapter(sql, con);
  242.             da.Fill(ds1, "UserList"); //creates a virtual database in memory called Userlist
  243.             MaxRows = ds1.Tables["UserList"].Rows.Count;
  244.  
  245.             //this checks if the username and password combination can be found
  246.             for (inc = 0; inc <= MaxRows - 1; inc++)
  247.             {
  248.                 DataRow dRow = ds1.Tables["UserList"].Rows[inc];
  249.                 CurrentUser = dRow.ItemArray.GetValue(1).ToString(); //here, 1 represents the 2nd field, 0 is the first
  250.                 CurrentPassword = dRow.ItemArray.GetValue(2).ToString();
  251.                 if ((CurrentUser == txtUser.Text) && (CurrentPassword == txtPass.Text)) //if the username and password is correct
  252.                 {
  253.                     Found = true;
  254.                     UserID = dRow.ItemArray.GetValue(0).ToString(); //Saves the user ID as a variable
  255.  
  256.                     string file_name = "user.ini"; //Writes to the .ini file here
  257.  
  258.                     System.IO.StreamWriter objWriter;
  259.                     objWriter = new System.IO.StreamWriter(file_name);
  260.  
  261.                     objWriter.Write(UserID);
  262.                     objWriter.Close();
  263.  
  264.                     frmMainMenu frm = new frmMainMenu(); //go to the next form
  265.                     frm.Show();
  266.                     this.Hide();
  267.                     break;
  268.                 }
  269.             }
  270.  
  271.             if (Found == false)
  272.             {
  273.                 MessageBox.Show("The Username or Password was incorrect, please try again.", "User Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  274.                 //if the username of password is wrong, then it will reset all the boxes and prompt the user to retry
  275.  
  276.                 txtUser.Text = "Username";
  277.                 txtUser.ForeColor = Color.Silver;
  278.                 txtNewUser.Text = "New Username";
  279.                 txtNewUser.ForeColor = Color.Silver;
  280.                 txtPass.Text = "Password";
  281.                 txtPass.ForeColor = Color.Silver;
  282.                 txtNewPass.Text = "New Password";
  283.                 txtNewPass.ForeColor = Color.Silver;
  284.                 txtPass.PasswordChar = '\0';
  285.                 txtNewPass.PasswordChar = '\0';
  286.                 txtUser.Focus();
  287.             }
  288.  
  289.             con.Close();
  290.             con.Dispose();
  291.  
  292.             /*
  293.            
  294.             frmMainMenu frm = new frmMainMenu();
  295.             frm.Show();
  296.             this.Hide();
  297.  
  298.             txtUser.Text = Username;
  299.             txtPass.Text = Password;
  300.            
  301.             */
  302.  
  303.             /*
  304.  
  305.             accountTable.DefaultView.Sort = "Username";
  306.             int foundRow = accountTable.DefaultView.Find(txtUser.Text);
  307.             if (foundRow == -1)
  308.             {
  309.                 MessageBox.Show("The username was not found in the database. You can create an account by clicking the appropriate button below.", "User Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
  310.                 txtUser.Text = "Username";
  311.                 txtUser.ForeColor = Color.Silver;
  312.                 txtNewUser.Text = "New Username";
  313.                 txtNewUser.ForeColor = Color.Silver;
  314.                 txtPass.Text = "Password";
  315.                 txtPass.ForeColor = Color.Silver;
  316.                 txtNewPass.Text = "New Password";
  317.                 txtNewPass.ForeColor = Color.Silver;
  318.                 txtPass.PasswordChar = '\0';
  319.                 txtNewPass.PasswordChar = '\0';
  320.                
  321.                 //txtUser.Text = "";
  322.                 //txtUser.Focus();
  323.                 //accountManager.Position = foundRow;
  324.             }
  325.             else
  326.             {
  327.                 //accountTable
  328.                 // Allow access if Password is correct
  329.                 frmMainMenu frm = new frmMainMenu();
  330.                 frm.Show();
  331.                 this.Hide();
  332.  
  333.              */
  334.  
  335.  
  336.             }
  337.         }
  338.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement