Why Is My C# Code Only Displaying The First Row from my Database?

5120bee

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?

Mark Kram

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why database first classes differ from my code first classes

From Dev

Why is my script is only getting the first row of my php table? below is my code

From Dev

Why is my recyclerview only displaying the content of the first item?

From Dev

Why my head id is not displaying the next automatic id from the database in the swing form? How to code mandatory fields?

From Dev

My Ajax code is only getting the value of one row from Mysql database rows

From Dev

Why isn't my code posting data from my database?

From Dev

How to update my database generated by "code first from database"?

From Dev

why setTimeout() only run my code once,at first time?

From Dev

Why is the compiler only compiling the first two sections of my code?

From Dev

Why is not displaying my list from my api on my react app?

From Dev

Why is my code not displaying these 2 Arraylists?

From Dev

Why is my padding not being removed only from the first child

From Dev

Why does my function only returns the first object from the array

From Dev

why my TCP server code send a SYN/ACK on only first packet or only on the first connection?

From Dev

Trying to print out all of my records from MYSQL database but only 1 record is displaying

From Dev

The first row of my SQLite Database is not getting fetched and displayed in my RecyclerView

From Dev

Why my TableView first row is selected?

From Dev

why is my image not displaying?

From Dev

Cant find my EF code first database

From Dev

only the first line of my javascript code works

From Dev

Why my listview only detects the first checkbox?

From Dev

Why is only my first HTTP request running?

From Dev

Why is my for only working on the first variable?

From Dev

Why is my for loop only grabbing first element?

From Dev

why my Codeigniter database query is only returning one row instead of more?

From Dev

Why is my list function only outputting first value of my array?

From Dev

Why is my code not reading the first line?

From Dev

Why isn't my JS code displaying the message to the HTML page?

From Dev

Why is my code displaying the same output every time?

Related Related

  1. 1

    Why database first classes differ from my code first classes

  2. 2

    Why is my script is only getting the first row of my php table? below is my code

  3. 3

    Why is my recyclerview only displaying the content of the first item?

  4. 4

    Why my head id is not displaying the next automatic id from the database in the swing form? How to code mandatory fields?

  5. 5

    My Ajax code is only getting the value of one row from Mysql database rows

  6. 6

    Why isn't my code posting data from my database?

  7. 7

    How to update my database generated by "code first from database"?

  8. 8

    why setTimeout() only run my code once,at first time?

  9. 9

    Why is the compiler only compiling the first two sections of my code?

  10. 10

    Why is not displaying my list from my api on my react app?

  11. 11

    Why is my code not displaying these 2 Arraylists?

  12. 12

    Why is my padding not being removed only from the first child

  13. 13

    Why does my function only returns the first object from the array

  14. 14

    why my TCP server code send a SYN/ACK on only first packet or only on the first connection?

  15. 15

    Trying to print out all of my records from MYSQL database but only 1 record is displaying

  16. 16

    The first row of my SQLite Database is not getting fetched and displayed in my RecyclerView

  17. 17

    Why my TableView first row is selected?

  18. 18

    why is my image not displaying?

  19. 19

    Cant find my EF code first database

  20. 20

    only the first line of my javascript code works

  21. 21

    Why my listview only detects the first checkbox?

  22. 22

    Why is only my first HTTP request running?

  23. 23

    Why is my for only working on the first variable?

  24. 24

    Why is my for loop only grabbing first element?

  25. 25

    why my Codeigniter database query is only returning one row instead of more?

  26. 26

    Why is my list function only outputting first value of my array?

  27. 27

    Why is my code not reading the first line?

  28. 28

    Why isn't my JS code displaying the message to the HTML page?

  29. 29

    Why is my code displaying the same output every time?

HotTag

Archive