How to make login system with no repetitive accounts

630 Views Asked by At

I have a problem with my code. I want to make a login system C# with no repetitive accounts. Can anyone help me do so? Help would be greatly appreciated!

    private void enterButton_Click(object sender, EventArgs e)
    {
        con.Open();

        SqlCommand da = new SqlCommand("SELECT * FROM RegTable", con);

        SqlDataReader reader = null;
        reader = da.ExecuteReader();
        while (reader.Read())
        {
            if (usernameTextBox.Text == (reader["Username"].ToString()) && PasswordtextBox.Text == (reader["Password"].ToString()))
            {
                MessageBox.Show("Welcome!");
                canForm x = new canForm();
                x.ShowDialog();
                this.Hide();
                con.Close();

            }

            else
            {
                MessageBox.Show("Account Doesn't Exist");
            }

        }

   }
2

There are 2 best solutions below

0
Velocoder On

You are makeing your own Membership sytem, so you should create Sessions table, and then store your Session informations like: LoggedUser, TimeLoggedOn. In each logOn you should then check if that user is already logged in.

Things to consider: session expiration (when user closes browser without loggingOut).

0
Azhar Khorasany On

In your user table in the database add a field called LoggedIn (true, false) and check on login whether for a user this field is set to true or false.

If a login session is expired then catch the session expired event and update this field t and set it to false.

Also catch application close event and do the same.

To access browser close event have a look at this How to capture the browser window close event?

Hope this helps.