Click here to Skip to main content
15,893,368 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a problem with this code and I don't know how to fix it.

What I have tried:

Delphi
ADODataSet1.Close;
  if ListFiltr1.ItemIndex = 0 then
    ADODataSet1.CommandText :=
      'select [EmployeeID],LastName,FirstName,Title,City from Employees where '
      + ListeChampTxt.Items[ListeChampTxt.ItemIndex] +'= "' + TxtCherche.text +
      '" order by ' + ListTrie.Items[ListTrie.ItemIndex] + TxtTrie;
   ADODataSet1.Active := True;
   ADODataSet1.Requery();
end;
Posted
Updated 30-Jul-17 21:22pm
v2

1 solution

Start by not doing that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Then, use the debugger to find out exactly what string you are generating: I'd suspect this bit:
SQL
... order by ' + ListTrie.Items[ListTrie.ItemIndex] + TxtTrie ...
Since you don't close the quotes, and I'm pretty sure the open quote isn't what you wanted anyway, but since it all depends on the data in your fields we can't tell - and neither can you without using the debugger to find out exactly what is being sent to the DB.
 
Share this answer
 
Comments
rezki_z 31-Jul-17 3:35am    
I have full code but this part show me error message and i hnow the problem is in +'="'+ but i don't hnow how to fix it
OriginalGriff 31-Jul-17 3:42am    
Then use the debugger to find out exactly what you are generating. Compare that with what you wanted to generate and the problem should be obvious.
rezki_z 31-Jul-17 3:48am    
This code show me this


select [EmployeeID],LastName,FirstName,Title,City from Employees where LastName= "anyname"
order by [EmployeeID] Asc
And this is not correct and i want this

select [EmployeeID],LastName,FirstName,Title,City from Employees where LastName= 'anyname'
order by [EmployeeID] Asc

But i don't know how to change it i try everything
OriginalGriff 31-Jul-17 3:59am    
"The code shows me this"
No, it doesn't. You are reading what you meant to write, not what you did wrote.
Use the debugger. It's not difficult, and it will show you exactly what your code actually did.

Don't be frightened of it - it's the greatest tool you have to play with!
rezki_z 31-Jul-17 4:05am    
Whay i need to find the problem and i already hnow the error but i don't know how to fix it and i already told you where is the error

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900