I have only used query with Database table. how to use a query for calculated field?
Note: I am still a beginner in Delphi.
Main Topics
Browse All TopicsI want to display a bar gragh with a calculated value using a TADODataset. This is connect with the database. Also created a new field "days" via field editor made it calcualted field.
So in my Form create procedure i have the below code.
mq_dstproj.Close;
mq_dstproj.CreateDataset;
mq_dstproj.Insert;
mq_dstproj.FieldByName('da
mq_dstproj.Post;
I get the below error when i run my application
"The Connection cannot be used to perform this operation. It is either closed or invalid in this context"
Thanks in advance.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here is a sample project i created I added a TDBChart with Bar series. Linked it with a dataset (Tadodataset). Created a calculated field 'days' int he dataset.
I just want a single bar chart with the value i assign to it.
Sorry, if i have not given enough information. Just ask me.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, TeEngine, Series, ExtCtrls, TeeProcs, Chart, DBChart;
type
TForm1 = class(TForm)
DBChart1: TDBChart;
Series1: TBarSeries;
ADODataSet1: TADODataSet;
ADODataSet1days: TIntegerField;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
ADODataSet1.Close;
ADODataSet1.CreateDataset;
ADODataSet1.Insert;
ADODataSet1.FieldByName('d
ADODataSet1.Post;
end;
end.
I can't understand following from your code:
ADODataSet1.Close;
ADODataSet1.CreateDataset;
ADODataSet1.Insert; // ''Also created a new field "days" via field editor made it calcualted field''
// this way you can't create new field - you are inserting new record in your table
Anyway I would do it as follows:
Drop an AdoQuery on your form,
connect it to your AdoConnection,
connect DBChart dataset to AdoQuery
procedure TForm1.FormCreate(Sender: TObject);
begin
with AdoQuery do
begin
Close;
Sql.Clear;
Sql.Add('alter my_table_name add days integer null'); // adding new field to your table
ExecSql;
Sql.Clear;
Sql.Add('update my_table_name set days = 20'); // make your calculations
Sql.Add(' where some_conditions ');
ExecSql;
Sql.Clear;
Sql.Add('select * from my_table_name');
Open;
end;
end;
if it's a static calculated field, why not use the sql in the TADOQuery ?
SELECT A.*, 20 AS DAYS FROM TABLE
for Calculated fields, you need to set the OnCalcFields event
this calcs Test field (calculated) as X + Y
procedure TForm1.ADOQuery1CalcFields
begin
Dataset.FieldByName('TEST'
Dataset.FieldByName('X').A
end;
you only set the calculated field in this event !
when adding calculated fields you will need to add all normal fields too.
invoke the field editor by double clicking the query component and then right click and add all fields.
The calculated field is added also using this editor, you can specify type, name and fieldlength.
the calculation can be done in the OnCalcFields event.
check this link: http://www.podgoretsky.com
Business Accounts
Answer for Membership
by: bokistPosted on 2009-02-27 at 09:31:11ID: 23757740
Why not use Query ?
Close dataset.....insert with TAdoQuery....open dataset.