Link to home
Create AccountLog in
Avatar of bilgehanyildirim

asked on

Invalid Argument error when query returns 0 row.


Delphi + Zeos + Mysql

when query returns 0 (zero) row it gives this stupid invalid argument error :(
Avatar of arnismit

on what row in the code, can you show code ?
Assuming you're using Delphi 5 and ADO components: Download ADOExpress Update Pack 2 from

This fix resolves issue: "...Most commonly, the following error is generated when closing an empty dataset: "Either EOF or BOF is True, or the current record has been deleted. ..."
Avatar of bilgehanyildirim


I'm using Delphi 7 + Zeos DB Components.

The row I mentioned is the result of the query. I mean the search comes up with no result.
yea i understand , but what do you do in code when the error is raised

are you trying to select a value from a field ? you should check wheter there is a record or not. so check the Bof and Eof properties.

Most common mistake is reading frmo a record when there is none.

Let me know if you got a different situation

procedure TfrmMain.edtRHSearchOrderKeyPress(Sender: TObject;
  var Key: Char);
  conditionalSQL : TStringList;
  if ((edtRHSearchOrder.Text <> '') AND (length(edtRHSearchOrder.Text) >= 4) AND (Key = #13)) then
    conditionalSQL := TStringList.Create;
      conditionalSQL.text := 'SELECT *'
        +'INNER JOIN `orders` ON (`customer`.`order_number` = `orders`.`number`)'
        +'(`customer`.`bpostcode` LIKE "%'+edtRHSearchOrder.Text+'%") OR'
        +'(`customer`.`spostcode` LIKE "%'+edtRHSearchOrder.Text+'%") OR'
        +'(`customer`.`bsurname` LIKE "%'+edtRHSearchOrder.Text+'%") OR'
        +'(`customer`.`ssurname` LIKE "%'+edtRHSearchOrder.Text+'%") OR'
        +'(`customer`.`card_number` LIKE "%'+edtRHSearchOrder.Text+'%") OR'
        +'(`customer`.`email` LIKE "%'+edtRHSearchOrder.Text+'%") OR'
        +'(`customer`.`order_number` LIKE "%'+edtRHSearchOrder.Text+'%") OR'
        +'(`customer`.`bphone` LIKE "%'+edtRHSearchOrder.Text+'%") OR'
        +'(`customer`.`sphone` LIKE "%'+edtRHSearchOrder.Text+'%")'
        +'GROUP BY `orders`.`number` ORDER BY `orders`.`number` DESC';
      dmQueries.queryRHViewOrders.SQL.Text := conditionalSQL.Text;
      edtRHSearchOrder.Text := '';

This is the query I'm running. (TZQuery). and there is DataSource related to this query and there is a string grid (DevExpress Quantum Grid) related to this dataset :):):):)

if the query can't find any record than it gives this error. if it finds at least one error, then there is no problem
what is that weird character you have all over the place?


this: `

have you tried taking those out?
if you run the application in the debugger where does it break when the exception is raised ?
ooo man, don't even think like that :)

have a look at this!

I wonder if the Zeos components handle that character well then...

from this:
dmQueries.queryRHViewOrders.SQL.Text := conditionalSQL.Text;

you can get the actual query text (Ctrl+F7 on "conditionalSQL.Text" when debugging), can you copy and paste that value into MySQL Control Center or whatever you use to query your MySQL database and does it work?
"you can get the actual query text (Ctrl+F7 on "conditionalSQL.Text" when debugging), "

Can you explain that part please? I don't know how to use Ctrl+F7
when you are debugging, stepping through your code with F7, F8

and you get to that point you can do Ctrl+F7 in a variable and it will display the "Evaluate/Modify" window, from there you can read the contents of any variables in that scope, in this case "conditionalSQL.Text" has the text that was generated from:

conditionalSQL.text := 'SELECT *'
      dmQueries.queryRHViewOrders.SQL.Text := conditionalSQL.Text; //put a breakpoint here, when it gets here, press Ctrl+F7 on the ConditionalSQL variable

that dialog allows you to copy the contents of that variable and you can paste it anywhere
there is nothing wrong with the SQL statement, b'cause I tried it with Navicat and PHPMuAdmin, and works perfect.
Avatar of kretzschmar
usual numberfields cannot be searched with like,
but as you said "there is nothing wrong with the SQL statement" . . .
yes I know but it works fine if it finds any record??
as you have no aggregat field in your select, you can delete the group by clause
Avatar of arnismit

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
btw. happens this error also outside of the IDE
(it may be internal handled, but captured by the IDE)
1. Delphi Throws an exception in ZAbstractRODataset.Pas File

function TZAbstractRODataset.CreateResultSet(SQL: string;
  MaxRows: Integer): IZResultSet;
    if not Assigned(Statement) then
      Statement := CreateStatement(FSQL.Statements[0].SQL, Properties);
    SetStatementParams(Statement, FSQL.Statements[0].ParamNamesArray,
      FParams, FDataLink);
    if RequestLive then  ***************************** >>>> HERE
    else Statement.SetResultSetConcurrency(rcReadOnly);
    if IsUniDirectional then
    else Statement.SetResultSetType(rtScrollInsensitive);
    if MaxRows > 0 then
    Result := Statement.ExecuteQueryPrepared;

2. "you get this error only on an empty resultset?
there may a translation-problem within zeos or delphi itself (usual a bug).

use a code like

  //nothing or handle here the invalid argument error
if myQry.IsEmpty then ....

meikl ;-)"

yes it only happent on an empty resultset and I did add the code you mentioned and nothing changed.

3. It happens both inside and outside of the IDE
>yes it only happent on an empty resultset and I did add
>the code you mentioned and nothing changed.

did you tried the change outside of the IDE?
>did you tried the change outside of the IDE?

try to change what?? I compiled the project, even copied to an other computer still "Invalid Argument" error.
well, this change had no effect -> problem must not be the empty resultset!

do you have any other datasources which are linked to the empty resultset like
LookupSources, Detail-Datasorces, etc.)?

meikl ;-)
No, that's the only Datasource.

If it is not Empty resultset  then how come it doesn't give any error. I will loose my hairssssssssss :(
In this instance, I would be checking ALL event handlers.

You may want to make a backup of the app and start removing forms/controls to see what can doesn't give you the error.

Do you have any event handlers dealing with parsing the result? Do you have any column formatting code, anything which HAS to access the data and may not be coded to deal with NO data?
I will start to check this right now. it will take so loooonnnggg :)
>I will loose my hairssssssssss

keep your hairs, was a guess.
furthermore guessing:
do you have assigned an afteropen, afterscroll, ondatachange-event to the dataset/datasource

btw. is the requestlive property true?

meikl ;-)
good point, RQuadling, didn't see your comment before ;-)

Yes, I'm now checking all event handlers associated with this query and datasource even with DevExporess Quantum grids event handlers.

yes, RequestLive is true.
Sorry. Been away for the weekend. Saw a LOT of movement here and didn't read all the comments. Sorry.
Thats what i asked you before bilgehanyildirim.

i asked "Are you sure there is no other code in the project thats expects an record after you called the open procedure "

didnt you check it before ?
I know arnismit, but I didn't have enough time during weekend...
Do you need the RequestLive!!! ????
turn it off, it will probably work then .....

>turn it off, it will probably work then .....
that would be my next step, too slow :-))
same error...

the problem was exactly you said. on AFTEROPEN eventhandler of that query there is another function which takes a parameter coming from result set. if the resultset is empty, this function crashes!!!!!!

You saved my hair :)
glad you got it sorted :-))

>You saved my hair :)
the last three one :-)) ?

meikl ;-)