My objective
Hi, I'm trying to code a C# application where I want to retrieve values from a table called Fruits_Table in a Microsoft Access 2016 database called FruitsDatabase.accdb. I then want to display these values in Labels in my form called label1, label2, label3.
Just for reference, I'm using a constructor called Fruit(string Name, string Color) in another class file called "Fruits.cs". I then pass this constructor into my PresentationGUI form.
My Database
The database has one table named Fruits_Table and in that table it has two columns: "Name" for the name of the fruit and "Color" for its color. THREE entries are recorded in the database: Apple - red, Banana - yellow, Pear - green.
What I Managed to get working so far
I managed to make a SUCCESSFUL connection to my database. So there's no need to worry about that. Retrieving values from the database is also good to go.
My Problem
The problem I have is this:
Only the FIRST entry in my database seems to be displaying no matter which fruit picture box I click. In other words, no matter if I click picBox1, picBox2, or picBox3 it ONLY ever displays 'Apple' and 'Red'
How do I go about iterating through the rows of my database?
using System;
...
using system.Windows.Forms;
using system.Data.OleDb;
namespace project1
{
public partial class PresentationGUI : Form
{
private OleDbConnection aConn;
private OleDbCommand aCmd;
private OleDbDataReader aReader;
private string aConnection;
private string sql;
Fruit[] aFruit = new Fruit[10];
public PresentationGUI()
{
Initialize Component;
//This is working fine.
aConnection = "Provider = Microsoft.ACE.OLEDB.12.0; Data Source = FruitsDatabase.accdb;";
sql = "SELECT * FROM Fruits_Table";
aCmd = new OleDbCommand();
aCmd.CommandText = sql;
aCmd.Connection = aConn;
aReader = aCmd.ExecuteReader();
while (aReader.Read())
{
//I have a Fruit constructor that goes: Fruit(string Name, string Color) in another class named Fruit.cs, and I use it here for this form.
aFruit[0] = new Fruit(aReader["Name"].ToString(), aReader["Color"].ToString());
aFruit[1] = new Fruit(aReader["Name"].ToString(), aReader["Color"].ToString());
aFruit[2] = new Fruit(aReader["Name"].ToString(), aReader["Color"].ToString());
}
private void PresentationGUI_Load(object sender, EventArgs e) {...}
//Intended Output: when the user clicks on picBox1 (showing an image of an apple) it should display 'Apple' for label1 and 'red' for label2
//Reality: it displays 'Apple' for label1 and 'Red' for label2
private void picBox1_Click(object sender, EventArgs e)
{
try
{
this.label1.Text = aFruit[0].Name.ToString();
this.label2.Text = aFruit[0].Color.ToString();
}
catch (NullReferenceException) {....}
}
//Intended Output: when the user clicks on picBox2 (showing an image of an Banana) it should display 'Banana' for label1 and 'yellow' for label2
//Reality: it displays 'Apple' for label1 and 'Red' for label2
private void picBox2_Click(object sender, EventArgs e)
{
try
{
this.label1.Text = aFruit[1].Name.ToString();
this.label2.Text = aFruit[1].Color.ToString();
}
catch (NullReferenceException) {....}
}
//Intended Output: when the user clicks on picBox2 (showing an image of an Pear) it should display 'Pear' for label1 and 'green' for label2
//Reality: it displays 'Apple' for label1 and 'Red' for label2
private void picBox3_Click(object sender, EventArgs e)
{
try
{
this.label1.Text = aFruit[2].Name.ToString();
this.label2.Text = aFruit[2].Color.ToString();
}
catch (NullReferenceException) {....}
}
}
}
Why does it only display the first row of my database?
You are loading the same value into your array three times, so try changing your While statement like this:
int fruitCounter = 0
while (aReader.Read())
{
//I have a Fruit constructor that goes: Fruit(string Name, string Color) in another class named Fruit.cs, and I use it here for this form.
aFruit[fruitCounter] = new Fruit(aReader["Name"].ToString(), aReader["Color"].ToString());
fruitCounter++;
if(fruitCounter > aFruit.Length)
{
break;
}
}
I would suggest that you first get the number of rows to be returned by your query and then initialize your array based upon that value.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments