0

I wrote an application in C# using Entity Framework 5.0, now I have username and password in database, I want to login with that data to my application but I can't.

This is where I'm stuck:

private void login_Click(object sender, EventArgs e)
{          
    Database1Entities connect = new Database1Entities();
    Users users = new Users();

    if (users.username == usernameTextBox.Text && users.password == passwordTextBox.Text)
    {
        Form3 f3 = new Form3();
        f3.Show();
        this.Hide();
    }
}
4
  • 1
    You have to first find one record by username, then you compare if the password is correct. And you would usually do this with hashed passwords, but i'd worry about that later. Commented Dec 29, 2021 at 22:37
  • But PLEASE don't store your password in clear text in your database table !!!! This is a HUGE security NO-GO ! Commented Dec 30, 2021 at 6:45
  • Also: Entity Framework is NOT a database - it's a data access library. The database might be SQL Server, Oracle, MySQL, PostgreSQL - whatever - but EF is not a database Commented Dec 30, 2021 at 6:46
  • Thank you for correcting me, im still learning Commented Dec 31, 2021 at 9:22

1 Answer 1

1

Try something like

    using var db = new Database1Entities();
    var user = db.Users.Where(u => u.UserName == usernameTextBox.Text).FirstOrDefault();

    if (user != null && user.password == passwordTextBox.Text)
    {
        Form3 f3 = new Form3();
        f3.Show();
        this.Hide();
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.