Iman Amien
Iman Amien

Reputation: 11

Dataset not in edit or insert mode

Im a new user of stack overflow. I have a big project coming up and i keep getting the same error. I’m trying to load data into my database and no matter how many times I try or what I change I still get the same errors. I have labled nessarry variables, tried tbl().Edit and tbl().Insert inside, outside of my loops and in the case statement but i still seem to be getting the same error.

sPlayer1,sPlayer2, iScore1,iScore2 are global variables. `

procedure TfrmInvigilator.btnSubmitClick(Sender: TObject);
Var
  i, j, iRound: Integer;
begin
  sPlayer1 := cmbName1.Text; // Assigning values to variables for cmbPlayers
  sPlayer2 := cmbName2.Text;

  iScore1 := sedScore1.Value; // Assigning values to variables for sedScores
  iScore2 := sedScore2.Value;

  iRound := cmbRound.ItemIndex; // Assigning values to variable for cmbRound

  if (cmbName1.ItemIndex = -1) then
  // Displays show message if cbmName1 is blank
  begin
    ShowMessage('Please select player name');
  end
  else
  begin // If cbmName1 is not blank then:
    i := pos(' ', sPlayer1); // Find position of ' ' in cbmName1.Text
    sPlayer1 := Copy(sPlayer1, 1, i - 1); // sPlayer1 := Name of player

    dmChess.tblPlayerInfo.Locate('Name', sPlayer1, []); // Locates Name in table
    Num1 := dmChess.tblPlayerInfo['ID']; // Retrives ID of player

    with dmChess do
    begin
      tblScoreboard.First;

      while NOT tblScoreboard.EOF do
      begin
        tblScoreboard.Locate('ID', Num1, []);

        case iRound of
          0:
              tblScoreboard['Round 1A'] := iScore1;
          1:
              tblScoreboard['Round 1B'] := iScore1;
          2:
              tblScoreboard['Round 1C'] := iScore1;
          3:
              tblScoreboard['Round 1D'] := iScore1;
          4:
              tblScoreboard['Round 2'] := iScore1;
          5:
              tblScoreboard['Semi-Final'] := iScore1;
          6:
              tblScoreboard['Final'] := iScore1;
        end;

        tblScoreboard.Post;
      end;
    end;
  end;

   if (cmbName2.ItemIndex = -1) then
   // Displays show message if cbmName2 is blank
   begin
   ShowMessage('Please slecet player name');
   end
   else
   begin // If cbmName2 is not blank then:
   j := pos(' ', sPlayer2); // Find position of ' ' in cbmName2.Text
   sPlayer2 := Copy(sPlayer2, 1, j - 1); // sPlayer2 := Name of player

   dmChess.tblPlayerInfo.Locate('Name', sPlayer2, []); // Locates Name in table
   Num1 := dmChess.tblPlayerInfo['ID']; // Retrives ID of player
   end;

end;

`

Upvotes: 0

Views: 640

Answers (1)

Softacom
Softacom

Reputation: 643

You can't just set a new value in a DataSet field (table or query), the DataSet must know that you want to change the current record or insert a new one. An exception tells you this. How to solve: before you make changes to any field in the dataset, you need to set the desired mode. DataSet.edit; - to edit the current record, DataSet.Insert - to insert a new record in the current location, DataSet.Append - to add a new record to the end. For this code snippet, add the line tblScoreboard.Edit; after the line tblScoreboard.Locate('ID', Num1, []); DataSet.Post – save current changes into record and change DataSet mode back to Browsing

Upvotes: 3

Related Questions