I am trying to mess around with a side project i have been meaning to complete for some time now and only just got back into it - Only remembering why i got over it in the first place.
I have created a login system connected to my access Database. All the logins work perfectly however each login has a given security level which should give them access to certain buttons/controls based on their level. I only have 2 levels at the moment - 5 and 10. If a user is assign access level 5 then it will take them to the main form where certain buttons/controls will not be accessable to them (Either not be there OR greyed out). If a user assigned with the access level 10 signs in, it will direct them to the main form where ALL buttons/controls will be accessable to them.
I have tried the below however it doesnt matter what acceess level i have assigned the user, It still gives all users access to use all buttons/controls.
using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OleDb;
namespace CCCAutoEngineering
{
public partial class FormLogin : Form
{
//CREATES A CONNECTION TO THE DATABASE
private const string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:/CCC/CCCDB.accdb;";
readonly OleDbConnection con = new OleDbConnection(conString);
OleDbCommand cmd;
FrmMain main = new FrmMain();
EngineDesc engineDesc = new EngineDesc();
EngineSpec engineSpec = new EngineSpec();
public FormLogin()
{
InitializeComponent();
}
private void BtnCancel_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("Are you sure you want to leave?","Exit",MessageBoxButtons.YesNo);
if(dialogResult == DialogResult.Yes)
{
Application.Exit();
}
}
private void BtnLogin_Click(object sender, EventArgs e)
{
//SQL STMT
cmd = new OleDbCommand("select * from Login where Username=@username and Password=@password", con);
//ADD PARAMS
cmd.Parameters.AddWithValue("@username", txtuserName.Text);
cmd.Parameters.AddWithValue("@password", txtPassword.Text);
con.Open();
OleDbDataReader dataReader = default(OleDbDataReader);
dataReader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if (string.IsNullOrEmpty(txtuserName.Text))
{
MessageBox.Show("Please enter your username");
}
else if (string.IsNullOrEmpty(txtPassword.Text))
{
MessageBox.Show("Please enter your password");
}
else
{
while (dataReader.Read() == true)
{
lbluser.Text = dataReader["Username"].ToString();
lblpwd.Text = dataReader["Password"].ToString();
lblLevel.Text = dataReader["Level"].ToString();
if (txtuserName.Text.Trim() == lbluser.Text.Trim() || (txtPassword.Text.Trim() == lblpwd.Text.Trim()))
{
if (lblLevel.Text == "10")
{
MessageBox.Show("You have successfully logged in");
this.Hide();
main.ShowDialog();
this.Close();
}
else if(lblLevel.Text == "5")
{
MessageBox.Show("You have successfully logged in");
this.Hide();
engineDesc.Disable_controls();
engineSpec.Disable_controls();
main.Disable_controls();
main.ShowDialog();
this.Close();
}
}
else
{
MessageBox.Show("Invalid username and or password!");
txtuserName.Clear();
txtPassword.Clear();
txtuserName.Focus();
}
}
dataReader.Close();
con.Close();
}
}
}
}
You will notice the Disable_controls() function i am using, this is just a custom function i created, see below:
public void Disable_controls() {
addToolStripMenuItem.Enabled = false;
updateToolStripMenuItem.Enabled = false;
deleteToolStripMenuItem.Enabled = false;
}
EngineDesc just gets data from the user adds it to a database like the make model of a engine then they can create a detailed folder linked to it which goes to a new form which is enginespec
That one is engine spec
I call these here
FrmMain main = new FrmMain();
EngineDesc engineDesc = new EngineDesc();
EngineSpec engineSpec = new EngineSpec();

