Error: You have specified an invalid column ordinal

3.1k Views Asked by At

i'm having an issue loading data from my database to my windows form. I'm using the code below to retrieve the information through a datareader and then set the retrieved information to the appropriate labels and pictureboxes but when the AirSpace form is show, I get the exception in the title. I've researched this a bit and have come to the conclusion that this exception is given when the application tries to access a ordinal outside of the bounds but that isn't valid in this example (I don't think).

If you need any further explanation or details, please ask. Thanks in advance.

Code:

private void AirSpace_Shown(object sender, EventArgs e)
    {
        string connectionString = "datasource=localhost;port=3306;username=********;password=********";
        Login login = new Login();
        using (MySqlConnection conn = new MySqlConnection(connectionString))
        {
            using (MySqlCommand cmd = conn.CreateCommand())
            {
                string select = "SELECT username, premium, picture FROM userinfo.users WHERE username = @username;";
                //                        (0)       (1)      (2)
                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(2));
                        //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(1) == "1")
                        {
                            AppVersionLabel.Text = "Premium";
                        }
                        else
                        {
                            AppVersionLabel.Text = "Free";
                        }
                     }
                }
            }
        }
1

There are 1 best solutions below

10
On

Columns oridnals starts with 0 not 1

string select = "SELECT username, premium, picture FROM userinfo.users WHERE username = @username;";
 //                        (0)       (1)      (2)

So the following line

ProfilePicture.Load(Reader.GetString(3));

should be:

ProfilePicture.Load(Reader.GetString(2));

See: 25.2.3.5. MySqlDataReader

25.2.3.5.5. GetString

Gets the value of the specified column as a String object.

Parameters: The zero-based column ordinal.

Returns: The value of the specified column.

EDIT:

You need to read through your DataReader like:

using (MySqlDataReader Reader = cmd.ExecuteReader())
{
    while (Reader.Read())
    {
        //Set the user's profile picture to the user's profile picture.
        ProfilePicture.Load(Reader.GetString(2));
        //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(1) == "1")
        {
            AppVersionLabel.Text = "Premium";
        }
        else
        {
            AppVersionLabel.Text = "Free";
        }
    }
}