简体   繁体   中英

How to display a selected record in a textbox

So I'm trying to put selected records from my table into the textboxes, more specifically, the user's first name and their ID.

textBox2 displays the first name and textBox3 displays the ID. Technically, it works, but it always displays the LAST records from the table, not the specific records that the user used to login.

Example, textBox2 displays "Bob" and textBox3 displays "2", when supposedly they should be displaying "Bill" and "1".

Thanks in advance for any help.

void login()
    {
        string firstName;
        string studentID;

        string path = "Data Source = LOCALHOST; Initial Catalog =library ; username='root';password=''";
        MySqlConnection con = new MySqlConnection(path);
        string sqlSelect = "select * from users";
        MySqlCommand sqlCommand = new MySqlCommand(sqlSelect, con);
        try
        {
            con.Open();
            using (MySqlDataReader sqlReader = sqlCommand.ExecuteReader())
            {
                while (sqlReader.Read())
                {
                    firstName = sqlReader["FirstName"].ToString();
                    studentID = sqlReader["ID"].ToString();
                    textBox2.Text = firstName;
                    textBox3.Text = studentID;
                }
            }
        }
        catch
        {
            con.Close();
        }
    }

Code for logging in the first form.

void login()
    {
        string userName;
        string password;
        string userType;

        string path = "Data Source = LOCALHOST; Initial Catalog =library ; username='root';password=''";
        MySqlConnection con = new MySqlConnection(path);
        string sqlSelect = "select * from users WHERE Username = '" + textBox1.Text + "' AND Password = '" + textBox2.Text + "'";
        MySqlCommand sqlCommand = new MySqlCommand(sqlSelect, con);

        try
        {
            con.Open();
            using (MySqlDataReader sqlReader = sqlCommand.ExecuteReader())
            {
                while (sqlReader.Read())
                {
                    userName = sqlReader["Username"].ToString();
                    password = sqlReader["Password"].ToString();
                    userType = sqlReader["UserType"].ToString();

                    if (textBox1.Text == userName)
                    {
                    if (textBox2.Text == password)
                        {
                            if (comboBox1.Text == userType)
                            {
                                if (userType == "Admin")
                                {
                                    MessageBox.Show("Logged in as Admin", "Login");
                                    Admin frmAdmin = new Admin();
                                    this.Hide();
                                    frmAdmin.Show();
                                }
                                else if (userType == "Student")
                                {
                                    MessageBox.Show("Logged in as Student", "Login");
                                    Student frmStudent = new Student();
                                    this.Hide();
                                    frmStudent.Show();
                                }
                            }
                        }
                    }
                }
            }
        }
        finally
        {
            con.Close();
        }


    }

You are selecting all users from the table. You need some WHERE logic to limit that result set to 1 row. Otherwise your while loop will iterate until the last record returned, and those will be the values displayed.

You are getting all users data on your query

string sqlSelect = "select * from users";

when you should get only the one from the user that logged in, eg

string sqlSelect = "select * from users WHERE id=<ID of user logged in>";

The while loop is overwriting/updating the textBox2 and textBox3 with the information of each user on the table, so when it finishes it's always on the last user. So, if you get only the user logged in, the information will be correctly shown. Hope I helped.

You are overriding TextBox values over and over again until you reach the last one. If you just want to show the first record in the text box use this query:

string sqlSelect = "select TOP 1 * from users";

And write column names instead of *. It increases the query speed

If you are searching for a specific user, try:

string sqlSelect = string.Format("select * from users where username={0}",username); //supposed the login-ed user name is saved in username

Or

string sqlSelect = string.Format("select * from users where id={0}",userId); //supposed the login-ed user id is saved in userId

Update

For keeping username and other information use a public string and use it in all the forms you need. You can initialize it in a separate class:

public static string Username;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM