0

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";
                        }
                    }
                }
            }
        }
Noah Cordoba
  • 5
  • 2
  • 8
  • 1
    "Not working" means what? – Arran Dec 11 '13 at 17:43
  • @Arran My apologies for being so vague. "Not working" means that when the form loads, the values from the reader aren't being loaded into the picturebox and the labels. – Noah Cordoba Dec 11 '13 at 17:49
  • Does the query work when you run it independently of your app? – Brian Dec 11 '13 at 18:02
  • @Arran I figured out that what I should have been doing is not doing all this on form load, but on form shown. When I do this, I get `You have specified an invalid column ordinal.` for `ProfilePicture.Load(UserProfilePictureLocation);` – Noah Cordoba Dec 11 '13 at 18:02
  • @Brian MySQL can use either. `Database = some_database` is used if you want to select a database to start with. `Datasource = some_datasource` is used to specify where the database is hosted. – Noah Cordoba Dec 11 '13 at 18:06
  • @NoahCordoba - I stand corrected. I will remove the comment. – Brian Dec 11 '13 at 18:07

1 Answers1

0

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";
   }
}
Bearcat9425
  • 1,580
  • 1
  • 11
  • 12
  • Updated the code i'm using. Using this code, it loads nothing into the labels and the picturebox. – Noah Cordoba Dec 11 '13 at 18:11
  • You could do a check to make sure you are returning results by using if(Reader.HasRows), if that is false then your query is failing and not returning results – Bearcat9425 Dec 11 '13 at 19:11
  • Also I think as well that Reader.GetString(0) not Reader.GetString(1) is actually your username. The indice starts at 0 according to http://dev.mysql.com/doc/refman/5.0/es/connector-net-examples-mysqldatareader.html – Bearcat9425 Dec 11 '13 at 19:32