Delphi 7 - What's wrong with this code? - Trying to delete an SQL row.

The code below is used when someone focus on a TreeList and chooses the popup menu 'Delete'.

procedure TfrmMain.DeleteTask;
var
Query: TADOQuery;
LId: String;
begin
  //Grabs the ID of the selected row
  if Assigned(cxTasks.FocusedNode) then
   LId := cxTasks.FocusedNode.Texts[cxTasksID.ItemIndex];

  //Attempts to delete that row
  try
    Query := TADOQuery.Create(nil);
    Query.Connection := conSql;
    Query.SQL.Add('Select * FROM Tasks WHERE id='+LId); 
    Query.Active := True;
    Query.Delete;
    Query.Active := False;
    qryTasks.Refresh; //Refresh 
    //Query.SQL.Add('DELETE FROM Tasks WHERE id='+LId); 
    //Query.ExecSQL;
  finally
    FreeAndNil(Query);
  end;
end;

Open in new window


However, when I run the code I get this error message:
 SQL
Any idea what's wrong here?

Thanks.
John86aAsked:
Who is Participating?
 
Ferruccio AccalaiConnect With a Mentor Senior developer, analyst and customer assistance Commented:
I guess that the error comes from the qryTasks.refresh
Use qryTasks.requery instead
0
 
Ephraim WangoyaCommented:

Use the code you disabled but use quotedstr
procedure TfrmMain.DeleteTask;
var
  Query: TADOQuery;
  LId: String;
begin
  //Grabs the ID of the selected row
  if Assigned(cxTasks.FocusedNode) then
   LId := cxTasks.FocusedNode.Texts[cxTasksID.ItemIndex];

  //Attempts to delete that row
  Query := TADOQuery.Create(nil);
  try    
    Query.Connection := conSql;
    Query.SQL.Add(Format('DELETE FROM Tasks WHERE id = %s', [QuotedStr(LId)]); 
    Query.ExecSQL;

    qryTasks.Refresh; //Refresh 
  finally
    FreeAndNil(Query);
  end;
end;

Open in new window

0
 
Ephraim WangoyaConnect With a Mentor Commented:

I think I missed one bracket

procedure TfrmMain.DeleteTask;
var
  Query: TADOQuery;
  LId: String;
begin
  //Grabs the ID of the selected row
  if Assigned(cxTasks.FocusedNode) then
   LId := cxTasks.FocusedNode.Texts[cxTasksID.ItemIndex];

  //Attempts to delete that row
  Query := TADOQuery.Create(nil);
  try    
    Query.Connection := conSql;
    Query.SQL.Add(Format('DELETE FROM Tasks WHERE id = %s', [QuotedStr(LId)]));
    Query.ExecSQL;

    qryTasks.Refresh; //Refresh
  finally
    FreeAndNil(Query);
  end;
end;
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
jimyXCommented:
What is the datatype of the column "ID" in the table "Tasks"?
procedure TfrmMain.DeleteTask;
var
  Query: TADOQuery;
  LId: String;
begin
  //Grabs the ID of the selected row
  if Assigned(cxTasks.FocusedNode) then
   LId := cxTasks.FocusedNode.Texts[cxTasksID.ItemIndex];

  //Attempts to delete that row
  try
    Query := TADOQuery.Create(nil);
    Query.Connection := conSql;
    Query.Active := False;
    Query.SQL.Text := 'DELETE FROM Tasks WHERE id=:TID';
    Query.ParamByName('TID').Value:=LId;
    Query.ExecSQL;
    qryTasks.Refresh; //Refresh 
  finally
    FreeAndNil(Query);
  end;
end;

Open in new window

0
 
John86aAuthor Commented:
Indeed, the error was due to Refresh, Requery solved it.
Thanks.
0
 
FrozeniceCommented:
hi,


its like deleting a certain row that is not existing anymore.

and why need to select?

you can simply do..


 try
    Query := TADOQuery.Create(nil);
    Query.Connection := conSql;
    Query.SQL.Add('DELETE FROM Tasks WHERE id='+LId);
    Query.ExecSQL;
  finally
    FreeAndNil(Query);
  end;






BR
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.