Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Welcome to the Descartes Login page
- //This form reads and writes to the database file called banking.mdb
- //It also writes to a .ini file storing a variable which will be called upon in other pages
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.Data.OleDb;
- using System.IO;
- namespace Descartes
- {
- public partial class frmLogin : Form
- {
- public frmLogin()
- {
- InitializeComponent();
- }
- int Check = 0;
- /*OleDbConnection banking;
- OleDbCommand accountCommand;
- OleDbDataAdapter accountAdapter;
- DataTable accountTable;
- OleDbCommand itemsCommand; //ignore this, this is the old code which was
- OleDbDataAdapter itemsAdapter; //a totally different approach to the database
- DataTable itemsTable;
- OleDbCommand purchasesCommand;
- OleDbDataAdapter purchasesAdapter;
- DataTable purchasesTable;*/
- OleDbConnection con;
- DataSet ds1; //these are the variables for the connection to the db
- OleDbDataAdapter da;
- int MaxRows = 0;
- int inc = 0;
- string CurrentUser; //these will be used to check if the user/pass was correct
- string CurrentPassword;
- string UserID;
- bool Found = false;
- private void tmrFadeIn_Tick(object sender, EventArgs e)
- {
- this.Opacity += 0.02; //fades the page in
- }
- private void tmrWait_Tick(object sender, EventArgs e)
- {
- tmrFadeIn.Enabled = false; //stops the fade in function to save memory
- }
- private void btnExit_Click(object sender, EventArgs e)
- {
- this.Close();
- Application.Exit(); //closes the application
- }
- private void btnNew_Click(object sender, EventArgs e)
- {
- //Basically switches between the create new user interface and the existing login interface
- if (Check == 0)
- {
- txtUser.Visible = false;
- txtNewUser.Visible = true;
- txtPass.Visible = false;
- txtNewPass.Visible = true;
- btnLogin.Visible = false;
- btnReset.Visible = false;
- btnCreateNew.Visible = true;
- btnNew.Text = "Back to log in";
- txtUser.Text = "Username";
- txtNewUser.Text = "New username";
- txtPass.Text = "Password";
- txtNewPass.Text = "New password";
- Check = 1;
- }
- else if (Check == 1)
- {
- txtUser.Visible = true;
- txtNewUser.Visible = false;
- txtPass.Visible = true;
- txtNewPass.Visible = false;
- btnLogin.Visible = true;
- btnReset.Visible = true;
- btnCreateNew.Visible = false;
- btnNew.Text = "Create a new account";
- txtUser.Text = "Username";
- txtNewUser.Text = "New username";
- txtPass.Text = "Password";
- txtNewPass.Text = "New password";
- Check = 0;
- }
- }
- private void btnCreateNew_Click(object sender, EventArgs e)
- {
- //have yet to do this code, could be done but we will see if i have time
- }
- private void txtNewPass_Enter(object sender, EventArgs e)
- {
- //When the new password box is selected, it will add a password mask to the box
- //Also, if the new username box is empty, it will reset the box to say New Username
- txtNewPass.PasswordChar = '*';
- if (txtNewPass.Text == "New Password")
- {
- txtNewPass.Text = "";
- txtNewPass.ForeColor = Color.Black;
- }
- if (txtNewUser.Text == "")
- {
- txtNewUser.Text = "New Username";
- txtNewUser.ForeColor = Color.Silver;
- }
- }
- private void txtPass_Enter(object sender, EventArgs e)
- {
- //When the password box is selected, it will add a password mask to the box
- //Also, if the username box is empty, it will reset the box to say Username
- txtPass.PasswordChar = '*';
- if (txtPass.Text == "Password")
- {
- txtPass.Text = "";
- txtPass.ForeColor = Color.Black;
- }
- if (txtUser.Text == "")
- {
- txtUser.Text = "Username";
- txtUser.ForeColor = Color.Silver;
- }
- }
- private void txtUser_Enter(object sender, EventArgs e)
- {
- //If the password box is empty, it will reset the box to say Password
- if (txtUser.Text == "Username")
- {
- txtUser.Text = "";
- txtUser.ForeColor = Color.Black;
- }
- if (txtPass.Text == "")
- {
- txtPass.PasswordChar = '\0';
- txtPass.Text = "Password";
- txtPass.ForeColor = Color.Silver;
- }
- }
- private void txtNewUser_Enter(object sender, EventArgs e)
- {
- //If the new password box is empty, it will reset the box to say New Password
- if (txtNewUser.Text == "New Username")
- {
- txtNewUser.Text = "";
- txtNewUser.ForeColor = Color.Black;
- }
- if (txtNewPass.Text == "")
- {
- txtNewPass.PasswordChar = '\0';
- txtNewPass.Text = "New Password";
- txtNewPass.ForeColor = Color.Silver;
- }
- }
- private void frmLogin_Load(object sender, EventArgs e)
- {
- //This coding was aided by the PizzaDB program
- /* banking = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " + Application.StartupPath + "\\..\\..\\banking.mdb");
- banking.Open();
- accountCommand = new OleDbCommand("SELECT * FROM account ORDER BY UserID", banking);
- accountAdapter = new OleDbDataAdapter();
- accountAdapter.SelectCommand = accountCommand;
- accountTable = new DataTable();
- accountAdapter.Fill(accountTable);
- itemsCommand = new OleDbCommand("SELECT * FROM items ORDER BY ItemID", banking);
- itemsAdapter = new OleDbDataAdapter();
- itemsAdapter.SelectCommand = itemsCommand;
- itemsTable = new DataTable();
- itemsAdapter.Fill(itemsTable);
- purchasesCommand = new OleDbCommand("SELECT * FROM purchases ORDER BY PurchaseID", banking);
- purchasesAdapter = new OleDbDataAdapter();
- purchasesAdapter.SelectCommand = purchasesCommand;
- purchasesTable = new DataTable();
- purchasesAdapter.Fill(purchasesTable);*/
- //ignore the above code, none of this was needed
- }
- private void btnReset_Click(object sender, EventArgs e)
- {
- //Resets the textboxes to their original values
- txtUser.Text = "Username";
- txtUser.ForeColor = Color.Silver;
- txtNewUser.Text = "New Username";
- txtNewUser.ForeColor = Color.Silver;
- txtPass.Text = "Password";
- txtPass.ForeColor = Color.Silver;
- txtNewPass.Text = "New Password";
- txtNewPass.ForeColor = Color.Silver;
- txtPass.PasswordChar = '\0';
- txtNewPass.PasswordChar = '\0';
- }
- private void btnLogin_Click(object sender, EventArgs e)
- {
- con = new OleDbConnection();
- ds1 = new DataSet();
- //this is the data connection
- con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " + Application.StartupPath + "\\..\\..\\banking.mdb"; //connection to database
- con.Open();
- string sql = "SELECT * From account"; //select everything from the account table
- da = new OleDbDataAdapter(sql, con);
- da.Fill(ds1, "UserList"); //creates a virtual database in memory called Userlist
- MaxRows = ds1.Tables["UserList"].Rows.Count;
- //this checks if the username and password combination can be found
- for (inc = 0; inc <= MaxRows - 1; inc++)
- {
- DataRow dRow = ds1.Tables["UserList"].Rows[inc];
- CurrentUser = dRow.ItemArray.GetValue(1).ToString(); //here, 1 represents the 2nd field, 0 is the first
- CurrentPassword = dRow.ItemArray.GetValue(2).ToString();
- if ((CurrentUser == txtUser.Text) && (CurrentPassword == txtPass.Text)) //if the username and password is correct
- {
- Found = true;
- UserID = dRow.ItemArray.GetValue(0).ToString(); //Saves the user ID as a variable
- string file_name = "user.ini"; //Writes to the .ini file here
- System.IO.StreamWriter objWriter;
- objWriter = new System.IO.StreamWriter(file_name);
- objWriter.Write(UserID);
- objWriter.Close();
- frmMainMenu frm = new frmMainMenu(); //go to the next form
- frm.Show();
- this.Hide();
- break;
- }
- }
- if (Found == false)
- {
- MessageBox.Show("The Username or Password was incorrect, please try again.", "User Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
- //if the username of password is wrong, then it will reset all the boxes and prompt the user to retry
- txtUser.Text = "Username";
- txtUser.ForeColor = Color.Silver;
- txtNewUser.Text = "New Username";
- txtNewUser.ForeColor = Color.Silver;
- txtPass.Text = "Password";
- txtPass.ForeColor = Color.Silver;
- txtNewPass.Text = "New Password";
- txtNewPass.ForeColor = Color.Silver;
- txtPass.PasswordChar = '\0';
- txtNewPass.PasswordChar = '\0';
- txtUser.Focus();
- }
- con.Close();
- con.Dispose();
- /*
- frmMainMenu frm = new frmMainMenu();
- frm.Show();
- this.Hide();
- txtUser.Text = Username;
- txtPass.Text = Password;
- */
- /*
- accountTable.DefaultView.Sort = "Username";
- int foundRow = accountTable.DefaultView.Find(txtUser.Text);
- if (foundRow == -1)
- {
- 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);
- txtUser.Text = "Username";
- txtUser.ForeColor = Color.Silver;
- txtNewUser.Text = "New Username";
- txtNewUser.ForeColor = Color.Silver;
- txtPass.Text = "Password";
- txtPass.ForeColor = Color.Silver;
- txtNewPass.Text = "New Password";
- txtNewPass.ForeColor = Color.Silver;
- txtPass.PasswordChar = '\0';
- txtNewPass.PasswordChar = '\0';
- //txtUser.Text = "";
- //txtUser.Focus();
- //accountManager.Position = foundRow;
- }
- else
- {
- //accountTable
- // Allow access if Password is correct
- frmMainMenu frm = new frmMainMenu();
- frm.Show();
- this.Hide();
- */
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement