Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 9 of 9 total
Thread Add AfterLoad procedure to DataSet created in Code
Wed, Jul 5 2017 4:08 PMPermanent Link

Big Al

I am wanting to manually create a dataset in code:

begin
  DSVerifyLogin := TDataSet.Create(nil);
  with DSVerifyLogin.Columns.Add do
     begin
     Name:='PersonID';
     DataType:=dtInteger;
     Length:=9;
     end;
end;

I need to be able to add the AfterLoad event for this dataset created in code.

I am sure it's easy for Object Pascal people, but I have not figure it out yet.

Second question if I may ask, I really want to have this dataset available in all my forms, etc, so I was thinking of putting it in a Unit or just MainForm if that makes more sense, and then trying to reference it from various forms, etc. Is this possible, and if so, how would I do it? I am hoping it can be done so that if the layout of the dataset changes, I can just change it in one place.

Thanks
Big Al
Wed, Jul 5 2017 4:34 PMPermanent Link

Raul

Team Elevate Team Elevate

On 7/5/2017 4:08 PM, Big Al wrote:
> I am wanting to manually create a dataset in code:
>
> begin
>     DSVerifyLogin := TDataSet.Create(nil);
>     with DSVerifyLogin.Columns.Add do
>        begin
>        Name:='PersonID';
>        DataType:=dtInteger;
>        Length:=9;
>        end;
> end;
>
> I need to be able to add the AfterLoad event for this dataset created in code.
>
> I am sure it's easy for Object Pascal people, but I have not figure it out yet.

Most components have events and you can just look in the EWB IDE object
inspector to see what they are called and then simply assign to it in code.

AfterLoad is called Afterload and i most standard callback that takes a
sender (you can always look at one in IDE to see these things):

Steps

declare a new procedure

procedure MyCustomEventForAL(Sender: TObject);

......
//implement it

procedure TForm1.MyCustomEventForAL(Sender: TObject);
begin
  //do your processing
end;

and to assign it you can just do

DSVerifyLogin .AfterLoad := MyCustomEventForAL;

you can do so when creating it.



> Second question if I may ask, I really want to have this dataset available in all my forms, etc, so I was thinking of putting it in a Unit or just MainForm if that makes more sense, and then trying to reference it from various forms, etc. Is this possible, and if so, how would I do it? I am hoping it can be done so that if the layout of the dataset changes, I can just change it in one place.

You can  do a new database to hold this.

Or just do a new unit that has this as a var

unit shared_datasets;
interface
uses WebData;
var
  DSVerifyLogin:TDataset;

implementation
end.

You need to make sure you actually create this on app startup.

And then in all your units just use

uses shared_datasets

....

and reference DSVerifyLogin


Raul


Wed, Jul 5 2017 5:16 PMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com

<< I am wanting to manually create a dataset in code:

begin
  DSVerifyLogin := TDataSet.Create(nil);
  with DSVerifyLogin.Columns.Add do
     begin
     Name:='PersonID';
     DataType:=dtInteger;
     Length:=9;
     end;
end;

I need to be able to add the AfterLoad event for this dataset created in code.

I am sure it's easy for Object Pascal people, but I have not figure it out yet. >>

You're making all of this harder than it needs to be.

Do this:

1) With your project open, either

a) drag the database from the Database Manager and drop it in the Units section of the Project Manager, or
b) select File/New/Database and create a new database in your project

Doing a) will cause all defined datasets in the database to automatically be created for you in the newly-created database.

If you do b)  then you can still drag and drop datasets from the Database Manager on to the database designer after-the-fact.

2) Save your project.

3) In the Project/Options dialog under the Forms and Databases tab, make sure that the database that you just created is listed first in the list of auto-created forms and databases.

4) Create any (additional) datasets that you wish in the database by either dragging and dropping the dataset from the Database Manager (as mentioned above) or by dragging and dropping a TDataSet instance from the component palette (under the Database tab) and dropping the dataset on the database designer.

5) Now you can set up the AfterLoad event handler.  Just double-click on the TDataSet that you created in the database designer and EWB will create an event handler skeleton for the AfterLoad event.  This is because the AfterLoad event is the default event for the TDataSet component.

Now, you can reference *any* of the datasets on your database from any forms in your project.  EWB will even make sure to add a reference to the database unit to the current unit when you select any of its TDataSet instances in the Object Inspector.

<< I am hoping it can be done so that if the layout of the dataset changes, I can just change it in one place.

Yes, performing the above will ensure that.  Also, 2.06 B3 has an improved dataset columns editor that allows for a reset of the dataset columns so that they are reconciled to match any changes done to the physical dataset *after* the dataset was added to the database.  You can access the dataset columns editor via the ... button in the Object Inspector next to the Columns property for the TDataSet instance.  See the 2.06 B3 release notes for more information.

Tim Young
Elevate Software
www.elevatesoft.com
Wed, Jul 5 2017 6:48 PMPermanent Link

Big Al

Raul wrote:

On 7/5/2017 4:08 PM, Big Al wrote:
> I am wanting to manually create a dataset in code:
>
> begin
>     DSVerifyLogin := TDataSet.Create(nil);
>     with DSVerifyLogin.Columns.Add do
>        begin
>        Name:='PersonID';
>        DataType:=dtInteger;
>        Length:=9;
>        end;
> end;
>

Raul, it appears as though this code may not actually work. I can compile and I have that code in the FormCreate, though I also tried it in the FormShow.

As soon as I open the form I get a Persistent load error (Invalid "PersonID" column length 9)
error. This is before I have clicked on the okay button so no webservice, or any other code would be running yet, just the formcreate.

I could have sworn there was an example somewhere that showed loading JSON to create the dataset I think, or maybe it used an existing dataset and loaded the columns. I could probably even use that, but I have not been able to find it again.

I am thinking the code above should work, I took it from the manual with only the tweaks I needed, but I must be doing something wrong.

Big Al
Wed, Jul 5 2017 7:21 PMPermanent Link

Big Al

Raul, I am taking baby steps so I did not try yet to put the code into a shared unit.

Big Al
Wed, Jul 5 2017 8:22 PMPermanent Link

Big Al

>>Raul said:

>>You need to make sure you actually create this on app startup.

I decided to go ahead and try the unit.

So I created the unit as you suggested. How do I actually create this on app startup like you said?

Big Al
Wed, Jul 5 2017 8:51 PMPermanent Link

Big Al

I found the example of loading JSON for the columns.

So my current code has the uSharedDatasets.

and then in my MainFormCreate, I have

X := '{ "columns": [
{ "name": "PersonID","type": 3,"length": 10,"scale": null }
] }';

DSVerifyLogin := TDataSet.Create(nil);
DSVerifyLogin.LoadColumns(X);


Which does compile, however at runtime I get the Persistent Load error (Invalid "PersonID" column length 10

So essentially the same error whether I use this code
  DSVerifyLogin := TDataSet.Create(nil);
  with DSVerifyLogin.Columns.Add do
     begin
     Name:='PersonID';
     DataType:=dtInteger;
     Length:=10;
     end;

or the other

X := '{ "columns": [
{ "name": "PersonID","type": 3,"length": 10,"scale": null }
] }';

DSVerifyLogin := TDataSet.Create(nil);
DSVerifyLogin.LoadColumns(X);

Big Al
Thu, Jul 6 2017 4:09 AMPermanent Link

Matthew Jones

Big Al wrote:

> As soon as I open the form I get a Persistent load error (Invalid "PersonID" column length 9)

Not sure, but I presume you have a grid or something that is set up to read from a database that doesn't exist yet.

--

Matthew Jones
Thu, Jul 6 2017 11:28 AMPermanent Link

Tim Young [Elevate Software]

Elevate Software, Inc.

Avatar

Email timyoung@elevatesoft.com


<< As soon as I open the form I get a Persistent load error (Invalid "PersonID" column length 9) error. >>

The error is exactly what it says: you're specifying a length for an Integer column.  Use this instead:

  X := '{ "columns": [{ "name": "PersonID","type": 3,"length": null,"scale": null }] }';

  DSVerifyLogin := TDataSet.Create(nil);
  DSVerifyLogin.LoadColumns(X);

Tim Young
Elevate Software
www.elevatesoft.com
Image