MySQLDataReader not loading any data into specified places on form shown

128 Views Asked by At

I'm having problems loading information from my database into labels and pictureboxes. I think my code is correct to do what i'm wanting but i'm guessing not since it's not working. Below is the code i'm using. For the picture column in my database, I store the picture's path, not the actual blob. If you need anymore relevant information, please ask.

Code:

private void AirSpace_Shown(object sender, EventArgs e)
    {
        string connectionString = "datasource=localhost;port=3306;username=Admin;password=August211989";
        Login login = new Login();
        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            using (MySqlCommand cmd = conn.CreateCommand())
            {
                string select = "SELECT username, email, premium, picture FROM userinfo.users WHERE username = @username;";
                //                        (1)      (2)     (3)      (4)
                conn.Open();
                cmd.CommandText = select;
                cmd.Parameters.AddWithValue("@username", login.UsernameTextBox.Text);
                using (MySqlDataReader Reader = cmd.ExecuteReader())
                {

                    while (Reader.Read())
                    {
                        //Set the user's profile picture to the user's profile picture.
                        ProfilePicture.Load(Reader.GetString(4));
                        //Set the username to the user's username
                        Username.Text = Reader.GetString(1);
                        //Set the app version to the user's version
                        if (Reader.GetString(3) == "1")
                        {
                            AppVersionLabel.Text = "Premium";
                        }
                        else
                        {
                            AppVersionLabel.Text = "Free";
                        }
                    }
                }
            }
        }
1

There are 1 best solutions below

3
On

You might try this. This looks to be a log in code as well so I would add logic to where if you loop more than one it throws an error cause there should only be 1 result.

While(Reader.Read()){
   //Set the user's profile picture to the user's profile picture.
   string UserProfilePictureLocation = Reader.GetString(3);
   ProfilePicture.Load(UserProfilePictureLocation);
   //Set the username to the user's username
   Username.Text = Reader.GetString(0);
   //Set the app version to the user's version
   if (Reader.GetString(2) == "1")
   {
        AppVersionLabel.Text = "Premium";
   }
   else
   {
        AppVersionLabel.Text = "Free";
   }
}