1
votes

I have a custom login form for my application, which gets the username and password from a database (MS Access). After the user selects their username and puts password and hits the login button the new form will appear. This is the login button click event I used:

OleDbCommand cmde = new OleDbCommand("SELECT * FROM Account WHERE username='"+ username.Text +"' and password='"+ passtxt.Text +"'", GetConnection());
OleDbDataReader reader = cmde.ExecuteReader();
if (reader.HasRows == true)
{
    MainForm frm = new MainForm(this);
    frm.Show();
    this.Hide();
}
else
{
    MessageBox.Show("Wrong Password!!!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

My question is how do I get the username/level which has been logged in on my new form? so I can hide some options on my form for non-admin users.

4

4 Answers

2
votes

You can add fields in your database in the Users table to determine what level and then test this field as testing the username and password

This link will help you any question you have?

1
votes

Essentially, @Maath Programmer has the solution. You could have another field let's say "isAdmin" and you would check against that value in the form.

So user signs in.

Load user data. eg. Store the user data is some variables, let's say a user class.

Make appropriate checks. eg. Is user admin (It's just if statements)

Apply checks. eg. hide some elements.

Continue...

0
votes

whenever you just call new form with show or show dialog method than pass your userid value with Constructor



    Form2 objForm2=new Form2(String userid)
objForm2.ShowDialog();


and in Form2 make new Constructor with string args.

you can do any operation in form2 based on your requirement with your userid

0
votes

Just create a user type column in your database which specifies the user level as admin or user. eg if user = "admin" then ad.show ac.show bd.show else if user ="user" then ax.show