hi guys just need some help in creating a sign up button or a register button and the code inside it. I already created a login form complete with username and password, problem is I do not know how I can create a login for it. I mean it's just a form that doesn't function, only thing it has right now is an exception handler ready incase an unregistered username and password is enter. any idea how I can make one? It has something to do with databases right? Help me plss. and here is my code, it was declared in my MainForm.cs Class, take not it is just a class, it is not the actual main class(the ones with static void main() that calls classes).

 public void LogInButtonClicked(object source, EventArgs e)
    {

        // First, clear the fields reflecting the
        // previous student's information.
        ClearFields();

        //  Display password dialog
        passwordDialog = new PasswordForm();
        passwordDialog.ShowDialog(this);

        string password = passwordDialog.Password;
        string id = passwordDialog.Id;
        passwordDialog.Dispose();

        // We'll try to construct a Student based on
        // the id we read, and if a file containing
        // Student's information cannot be found,
        // we have a problem.

        currentUser = new Student(id + ".dat");
        currentUser.ReadStudentData(schedule);

        // Test to see if the Student fields were properly
        // initialized. If not, reset currentUser to null
        // and display a message box

        if (!currentUser.StudentSuccessfullyInitialized())
        {
            // Drat!  The ID was invalid.
            currentUser = null;

            // Let the user know that login failed,
            string message = "Invalid student ID; please try again.";
            MessageBox.Show(message, "Invalid Student ID",
                     MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        else
        {
            // We have a valid Student.  Now, we need
            // to validate the password.

            if (currentUser.ValidatePassword(password))
            {

                // Let the user know that the login succeeded.
                string message =
                     "Log in succeeded for " + currentUser.Name + ".";
                MessageBox.Show(message, "Log In Succeeded",
                       MessageBoxButtons.OK, MessageBoxIcon.Information);

                // Load the data for the current user into the TextBox and
                // ListBox components.
                SetFields(currentUser);
            }
            else
            {
                // The id was okay, but the password validation failed;
                // notify the user of this.
                currentUser = null;
                string message = "Invalid password; please try again.";
                MessageBox.Show(message, "Invalid Password",
                       MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        // Check states of the various buttons.
        ResetButtons();
    }

Edited 4 Years Ago by king03

You can store your username and password in your database and retrieve it and do a check. Or if you this is one of those project where you just want to test functionality, you can always hardcode your username and password and do a check. That all depend on what you really want to do.

hey wen cai thanks, still trying to figure out what to do, I created a database for it and I'm about to make it work. goodluck to me! hahah. thank you!

SqlConnection conn = new SqlConnection("server=KISH;uid=sa;pwd=msde;database=logdb");
SqlDataReader read;
SqlParameter usernameParam;

query = new SqlCommand();
query.CommandText = "SELECT PASSWORD FROM USERINFO WHERE USERNAME = @USERNAME";
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = conn;

usernameParam = new SqlParameter();
usernameParam.ParameterName = "@CITY";
usernameParam.SqlDbType = SqlDbType.VarChar;
usernameParam.Size = 15;
usernameParam.Direction = ParameterDirection.Input;
usernameParam.Value = usernameTxtBox.Text;

try
{
    conn.Open();

    read = query.ExecuteReader();

    while (read.Read())
    {
        if (read["password"].equals(passwordTxtBox.Text)){
            //Goes to the user page?
        }
        else
        {
            //Display error message?
        }
    }

    read.Close();
}
catch (SqlException se)
{
    Console.WriteLine ("SQL Exception: {0}", se.Message);
}
    catch (Exception e)
{
    Console.WriteLine ("Exception: {0}", e.Message);
}
finally
{
    if (conn.State == ConnectionState.Open)
    {
        conn.Close();
    }
}

You can try the code above that read the data from the database. I am not sure how your database is structure so make the changes to suit your database structure. Please change the connection string when you are trying the code too. Good luck in your endevour.

hey there wen cai what's the purpose of @CITY? line 11

This article has been dead for over six months. Start a new discussion instead.