Hello. I couldn't think of any better name for the title.
To the point...

I have this database reader method, and In the while loop, where the liga[] is being populated, all its values are the last that is read from DB..
Clubs ID are from 1 - 10
Any suggestions?

private static bool readDatabase(ref string strError, Club[] liga)
        {
            bool blnRetVal = true;
            Player player = new Player();
            Club club = new Club();
            string selectSQL = "SELECT * FROM Players";
            string selectClub = "SELECT * FROM Clubs";

            try
            {
                using (OleDbConnection conn = new OleDbConnection(connectionString))
                {
                    conn.Open();
                    using (OleDbDataReader rdr = (new OleDbCommand(selectClub, conn)).ExecuteReader())
                    {
                        while (rdr.Read()) // THIS IS WHERE THE PROBLEM STARTS
                        {
                            club.Id = int.Parse(rdr["IDclub"].ToString());
                            club.clubName = rdr["ClubName"].ToString().Trim();
                            club.Pld = int.Parse(rdr["Pld"].ToString());
                            club.W = int.Parse(rdr["W"].ToString());
                            club.D = int.Parse(rdr["D"].ToString());
                            club.L = int.Parse(rdr["L"].ToString());
                            club.Gf = int.Parse(rdr["Gf"].ToString());
                            club.Ga = int.Parse(rdr["Ga"].ToString());
                            club.Gd = int.Parse(rdr["Gd"].ToString());
                            club.Money = int.Parse(rdr["Money"].ToString());
                            liga[club.Id-1] = club;
                        }
                        rdr.Close();
                    }
                    using (OleDbDataReader rdr = (new OleDbCommand(selectSQL, conn)).ExecuteReader())
                    {
                        while (rdr.Read()) // Fill players
                        {
                            player.Name = rdr["name"].ToString().Trim();
                            player.Surname = rdr["surname"].ToString().Trim();
                            player.Position = rdr["position"].ToString().Trim();
                            player.Age = int.Parse(rdr["age"].ToString().Trim());
                            player.DaysInj = int.Parse(rdr["daysInj"].ToString().Trim());
                            player.ClubPlayingFor = int.Parse(rdr["Club"].ToString());
                            player.IdPlayer = int.Parse(rdr["IDplayer"].ToString());

                            player.Creativity = int.Parse(rdr["creativity"].ToString());
                            player.Shooting = int.Parse(rdr["shooting"].ToString());
                            player.Passing = int.Parse(rdr["creativity"].ToString());
                            player.Speed = int.Parse(rdr["speed"].ToString());
                            player.Jump = int.Parse(rdr["jump"].ToString());
                            player.Head = int.Parse(rdr["head"].ToString());
                            player.Defence = int.Parse(rdr["goalkeeping"].ToString());
                            player.Creativity = int.Parse(rdr["creativity"].ToString());

                            foreach (Club c in liga)
                            {
                                if (player.ClubPlayingFor == c.Id)
                                {
                                    c.PlayersBL.Add(player);
                                }
                            }
                        }
                        rdr.Close();
                    }
                    conn.Close();
                }
            }
            catch (Exception exc)
            {
                blnRetVal = false;
                strError = exc.Message;
            }

            MessageBox.Show(strError.ToString());
            return blnRetVal;
        }

Edited 4 Years Ago by niggz: n/a

Your design is only reading the data into ONE club object.
You would need to call "new" on each loop through the database.

[at some point...]
I recommend modifying the Club class to accept an IDataReader in a constructor.
As you loop through the table rows, just call:

Club tempClub = new Club(reader);
   liga[tempClub.Id-1] = tempClub;

[at some other point...]
I recommend using a List<Club> rather than an array

Edited 4 Years Ago by thines01: n/a

Okay, it works just fine.. :)

private static bool readDatabase(ref string strError, Club[] liga)
        {
            bool blnRetVal = true;
            

            //Club[] liga = new Club[10];
            // Define the Select statement.
            string selectSQL = "SELECT * FROM Players";
            string selectClub = "SELECT * FROM Clubs";

            try
            {
                using (OleDbConnection conn = new OleDbConnection(connectionString))
                {
                    conn.Open();
                    using (OleDbDataReader rdr = (new OleDbCommand(selectClub, conn)).ExecuteReader())
                    {
                        int inx = 0;
                        while (rdr.Read()) // Fill clubs
                        {
                            Club club = new Club(); //Moved from the top of the function, to here
                            club.Id = int.Parse(rdr["IDclub"].ToString());
                            club.clubName = rdr["ClubName"].ToString().Trim();
                            club.Pld = int.Parse(rdr["Pld"].ToString());
                            club.W = int.Parse(rdr["W"].ToString());
                            club.D = int.Parse(rdr["D"].ToString());
                            club.L = int.Parse(rdr["L"].ToString());
                            club.Gf = int.Parse(rdr["Gf"].ToString());
                            club.Ga = int.Parse(rdr["Ga"].ToString());
                            club.Gd = int.Parse(rdr["Gd"].ToString());
                            club.Money = int.Parse(rdr["Money"].ToString());
                            liga[inx] = club;
                            inx++;
                        }
                        rdr.Close();
                    }
                    using (OleDbDataReader rdr = (new OleDbCommand(selectSQL, conn)).ExecuteReader())
                    {
                        while (rdr.Read()) // Fill players
                        {
                            Player player = new Player();//Moved from the top of the function, to here
                            player.Name = rdr["name"].ToString().Trim();
                            player.Surname = rdr["surname"].ToString().Trim();
                            player.Position = rdr["position"].ToString().Trim();
                            player.Age = int.Parse(rdr["age"].ToString().Trim());
                            player.DaysInj = int.Parse(rdr["daysInj"].ToString().Trim());
                            player.ClubPlayingFor = int.Parse(rdr["Club"].ToString());
                            player.IdPlayer = int.Parse(rdr["IDplayer"].ToString());

                            player.Creativity = int.Parse(rdr["creativity"].ToString());
                            player.Shooting = int.Parse(rdr["shooting"].ToString());
                            player.Passing = int.Parse(rdr["creativity"].ToString());
                            player.Speed = int.Parse(rdr["speed"].ToString());
                            player.Jump = int.Parse(rdr["jump"].ToString());
                            player.Head = int.Parse(rdr["head"].ToString());
                            player.Defence = int.Parse(rdr["goalkeeping"].ToString());
                            player.Creativity = int.Parse(rdr["creativity"].ToString());

                            foreach (Club c in liga)
                            {
                                if (player.ClubPlayingFor == c.Id)
                                {
                                    c.PlayersBL.Add(player);
                                }
                            }
                        }
                        rdr.Close();
                    }
                    conn.Close();
                }
            }
            catch (Exception exc)
            {
                blnRetVal = false;
                strError = exc.Message;
            }

            MessageBox.Show(strError.ToString());
            return blnRetVal;
        }
This question has already been answered. Start a new discussion instead.