We help IT Professionals succeed at work.

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

Medium Priority
543 Views
Last Modified: 2012-05-11
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.
Comment
Watch Question

Ephraim WangoyaSoftware Engineer
CERTIFIED EXPERT

Commented:

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

Ephraim WangoyaSoftware Engineer
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CEO and Co-founder
CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Top Expert 2011

Commented:
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

Author

Commented:
Indeed, the error was due to Refresh, Requery solved it.
Thanks.
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
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.