1

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");
            }

        }

   }
Emalton
  • 5
  • 3
azzyloth
  • 43
  • 3
  • 12
  • 1
    Selecting ALL records from table and then running loop on that isn't good idea. Make use of WHERE parameter. – Dariusz Oct 10 '12 at 13:29
  • Do you want to prevent multiple logins from different locations or prevent multiple windows and tabs on the same system? – jTC Oct 10 '12 at 13:51

2 Answers2

0

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).

Dariusz
  • 15,573
  • 9
  • 52
  • 68
0

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.

Community
  • 1
  • 1
Azhar Khorasany
  • 2,712
  • 16
  • 20