1

I've connected a SQL database called Data to a windows form application in visual studio C#, the database contains one table called Login.

The table displays like this:

Link to image of my table

Here is image link to how data looks inside my table, so basically I will have Username, Password and Role (I have two types of roles which means user is either Admin or Client):

Image link to the data in my table

I have made a Login form called Login where users can enter username and password from the database which will take them to a form I've called AdminMenu. I have also made a Form called MenuForm, which I want users whose role is Client to be directed to when they login.

The problem is that I have only made the login so that all users will be directed to the AdminMenu when logging in even if their role isn't Admin. Can someone adapt my code so that users who have the role of Client will be directed to MenuForm when logging in, whereas users that have the role of Admin will be directed to AdminMenu form.

Here's is my code for Login Form:

    private void button2_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    private void button3_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|Data.mdf;Integrated Security=True");
        SqlDataAdapter sda = new SqlDataAdapter("Select Role from Login Where UserName='" + textBox1.Text + "' and Password='" + textBox2.Text + "'   ",con);
        DataTable dt = new System.Data.DataTable();
        sda.Fill(dt);
        if(dt.Rows.Count == 1)
        {
            this.Hide();
            AdminMenu ss = new AdminMenu();
            ss.Show();

        }

    }

    private void Login_Load(object sender, EventArgs e)
    {

    }
}

}

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
reddevil54
  • 53
  • 2
  • 9
  • Possible duplicate of [How can I redirect to a URL?](http://stackoverflow.com/questions/6985068/how-can-i-redirect-to-a-url) – Juan Carlos Oropeza Nov 11 '15 at 00:02
  • The question isn't similar to how do i redirect to a URL. As i want users to be directed to a specific form when logging in depending on their role in the database, not to a specific webpage like the other question asked. – reddevil54 Nov 11 '15 at 00:29

1 Answers1

1

Arguably there's no good reason for using a DataAdapter here, and you should better protect against injection attacks, and you may want to consider password salting, and the form parenting may need consideration, but based on your code the following should work :

// ...
if(dt.Rows.Count == 1)
{
    switch (dt.Rows[0]["Role"] as string)
    {
        case "Admin":
        {
            this.Hide();
            AdminMenu ss = new AdminMenu();
            ss.Show();
            break;
        }

        case "Client":
        {
            this.Hide();
            MenuForm mf = new MenuForm();
            mf.Show();
            break;
        }

        default:
        {
            // ... handle unexpected roles here...
            break;
        }
    }
}

You could otherwise execute the command without an adapter thus :

var cmd = conn.CreateCommand();
cmd.CommandText = "Select Role from Login Where UserName=@user and Password=@password";
var up = cmd.CreateParameter();
up.ParameterName="@user";
up.Value = textBox1.Text;
cmd.Parameters.Add(up);
var pp = cmd.CreateParameter();
pp.ParameterName="@password";
pp.Value = textBox2.Text;
cmd.Parameters.Add(pp);

var role = cmd.ExecuteScalar() as string;
// further processing... - role will be null if user not found
Ben Jackson
  • 1,108
  • 6
  • 9
  • Thank you Ben Jackson for answer, it works and i can now login into two different forms depending on the user type. I've also tried using your way without adapter but i get one error which displays the name conn does not exist in the current context. – reddevil54 Nov 11 '15 at 01:58
  • Sorry, it should be con (your connection object) rather than conn. – Ben Jackson Nov 11 '15 at 13:02
  • How and where would i add validation for the login form code so that it could include a messagebox displaying that username or password is incorrect when user enters wrong information when trying to log in? – reddevil54 Nov 14 '15 at 00:03