views:

488

answers:

5

Hi! Sorry for my english, but i hope you'll understand me :P

I'm trying to create new TSQLQuery component in code, without placing it on form. I wrote that code:

var
sql:tsqlquery;
pole:TFMTBCDField;

....
   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   pole:=TFMTBCDField.Create(self);
   pole.Name:='sqlilerap';
   pole.FieldName:='COUNT(idrap)';
   pole.FieldKind:=fkData;
   pole.DisplayLabel:='COUNT(idrap)';

   sql.Fields.Add(pole);
   sql.Open;
   showmessage(sql.FieldByName('COUNT(idrap)').AsString);
   sql.Free;
   pole.Free;

but i'm getting exception when i try to access data:

First chance exception at $75999617. Exception class EDatabaseError with message 'Field 'COUNT(idrap)' has no dataset'. Process htstrm2.exe (2308)

What should I do ?

A: 

Your database driver reports the empty field name for the aggregate expression.

Alias your field:

   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) AS cnt FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   pole:=TFMTBCDField.Create(self);
   pole.Name:='sqlilerap';
   pole.FieldName:='cnt';
   pole.FieldKind:=fkData;
   pole.DisplayLabel:='cnt';

   sql.Fields.Add(pole);
   sql.Open;
   showmessage(sql.FieldByName('cnt').AsString);
   sql.Free;
   pole.Free;
Quassnoi
nope, didnt help. Still error:First chance exception at $75999617. Exception class EDatabaseError with message 'Field 'cnt' has no dataset'. Process htstrm2.exe (2864)
WombaT
A: 

Try adding this line to your query:

AND idrap <> nil
Mihaela
but, its not a query problem (i think), query executed in phpmyadmin works without problems, and returns values between 2 and 16.
WombaT
I think that some values of idrap in your dataset might be nil. Some database engines won't do aggregates if all values upon which aggregates are calculated are not <> nil.
Mihaela
it didnt help. idrap is a primary key column in table so it cant be nil (null?), it always have a value.
WombaT
Did you traverse the dataset with sql.First, sql.Next? You just executed the query.
Mihaela
A: 

You must explicitly assign the dataset to the field, try adding this line

pole.DataSet:=sql;

Bye.

RRUZ
after that i receiver "BCD overflow" error
WombaT
you must change the field type from TFMTBCDField to TIntegerField
RRUZ
A: 

Don't even make a field. Queries such as this return one and only one field. So just reference from the fields array:

var
sql:tsqlquery;

....
   sql:=tsqlquery.Create(self);
   sql.SQLConnection:=ddm.konekszyn;
   sql.SQL.Text:='SELECT COUNT(idrap) FROM raporty WHERE idkier="'+lvkierowcy.Selected.Caption+'";';
   sql.Open;
   showmessage(sql.fields[0].AsString);
   sql.Free;
M Schenkel
Thanks, it just works :D didnt know that i dont have to add fields. Thank you !
WombaT
A: 

Alias the column being returned. You can then access it by that aliased name:

sql.SQL.Text:='SELECT COUNT(idrap) AS iDrapCount FROM raporty WHERE dkier 
="'+lvkierowcy.Selected.Caption+'";';
....
pole.FieldName := 'iDrapCount';
Ken White
alias isnt helping:(
WombaT
You need to be more specific. What does "isn't helping" mean? Are you getting an error message? Is your computer throwing up it's hands and saying "No way I'm helping you"? Providing nothing useful means that's what you get for help - nothing useful.
Ken White
inst helping in this case means that nothing changes after making an alias, still same error
WombaT
Can you single step through the code in the debugger and show exactly which line is causing the exception?
Ken White
showmessage(sql.FieldByName('COUNT(idrap)').AsString); here is a problem, but now its solved, as M Schenkel written above, a field isnt required. Thanks for trying to help. :)
WombaT