title: ASP.NET/SQL Function not storing all values in a loop
body: <p>So I'm creating a monopoly game, and I've got two functions which read/write information about the game to/from a database so it can be retrieved between postbacks. I also have a start game function which initialises everything to 0 and then calls the writeToDatabase(). createArrays() just creates a list of players/squares, squares have been removed as they'not relevant.  </p>
  
  <p>The problem I'm having is I believe it is only storing some of the values within the loop. When the start game function is called it correctly sets the  players positions to  0, and their money to 1500, however for some reason when reading from the database again the 4th players details immediately go back to what they were during the previous session.</p>
  
  <pre><code>        private void readFromDatabase()
          {
              createArrays();
  
              for (int i = 0; i &lt; players.Count(); i++)
              {
                  players[i].SetID(Convert.ToInt32(players_table.GetRow(i)["ID"]));
                  players[i].SetPosition(Convert.ToInt32(players_table.GetRow(i)["position"]));
                  players[i].SetMoney(Convert.ToInt32(players_table.GetRow(i)["money"]));
                  players[i].SetInJail(Convert.ToBoolean(players_table.GetRow(i)["isInJail"]));
                  players[i].SetIsBankrupt(Convert.ToBoolean(players_table.GetRow(i)["isBankrupt"]));
              }
  
              currentPlayerID = Convert.ToInt32(gameinfo_table.GetRow(0)["CurrentPlayerID"]);
              currentSquareID = Convert.ToInt32(gameinfo_table.GetRow(0)["CurrentSquareID"]);
              freeParkingAmount = Convert.ToInt32(gameinfo_table.GetRow(0)["FreeParkingAmount"]);
  
              currentPlayer = players[currentPlayerID];
              CurrentSquare = squares[currentSquareID];
          }
  
          private void writeToDatabase()
          {
              CurrentSquare = squares[currentSquareID];
  
              for (int i = 0; i &lt; players.Count(); i++)
              {
                  SQLDatabase.DatabaseRow prow = players_table.GetRow(players[i].GetID());
  
                  prow["ID"] = players[i].GetID().ToString();
                  prow["position"] = players[i].GetPosition().ToString();
                  prow["money"] = players[i].GetMoney().ToString();
                  prow["isInJail"] = players[i].IsInJail().ToString();
                  prow["isBankrupt"] = players[i].IsBankrupt().ToString();
  
                  players_table.Update(prow);
              }
  
              SQLDatabase.DatabaseRow girow = gameinfo_table.GetRow(0);
  
              girow["CurrentPlayerID"] = currentPlayerID.ToString();
              girow["CurrentSquareID"] = CurrentSquare.GetID().ToString();
              girow["FreeParkingAmount"] = freeParkingAmount.ToString();
  
              gameinfo_table.Update(girow);
          }
  
          private void startGame()
          {
              createArrays();
              currentPlayerID = 0;
              currentPlayer = players[0];
              CurrentSquare = squares[0];
              writeToDatabase();
          }
  
          private void createArrays()
          {
              for (int i = 0; i &lt; 4; i++)
              {
                  Player player = new Player(i);
                  players.Add(player);
              }
          }
  </code></pre>
  
  <p>I really can't work out why this is happening, as when it creates the player array it loops through 4 times, therefore creating 4 brand new players, and immediately overwriting the details in the database. It just seems odd it works perfectly fine for the first 3 players but the 4ths details don't seem to change. Any help would be greatly appreciated, I'm sure it something simple but I have spent hours on this and haven't got anywhere.</p>