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";
}
}
}
}
}
Columns oridnals starts with
0
not1
So the following line
should be:
See: 25.2.3.5. MySqlDataReader
EDIT:
You need to read through your DataReader like: