1. Home
  2. Computing & Technology
  3. Delphi Programming
Behind data in datasets
Page 1: The State of Data
 More of this Feature
• Page 2: From BOF to EOF
 Join the Discussion
"Post your views and comments to this chapter of the free Delphi database Programming Course"
Discuss!
 Related Resources
• free DB Course.TOC
• Database tutorials
• Selecting Fields.howto

When developing database applications with Delphi and ADO, most of the work is done with the help of dataset components. To create an ADO based application, Delphi provides us with several dataset components. TAdoTable, TAdoQuery and others are all designed to retrieve, present and modify the data inside a database table or query.
In this fifth chapter of the free database course we'll see exactly how to present, navigate and read the data - by looking at some of the most interesting datasets properties, events and methods.

Pick, set, connect and get
Since this is the fifth chapter, you should be familiar with the steps needed to create a database form. Back in the fourth chapter we have created, by hand, a simple data browsing form. The same form can be used to follow the discussion in this chapter.

The only (ADO) dataset component we used, by now, was TAdoTable. It's important to know that both TADOQuery and TADODatSet (as dataset components) share the same set of common methods and events.

   Open Sesame ; Close Sesame
One of the great features of Delphi database development is that Delphi enables us to work directly with the data while in design-mode. If you recall - in the previous chapters we used the Active property at design time to open the live connection with the data.
It's understandable, that prior to working with the data in a table, an application must first open a dataset. Delphi has two methods of performing this function. As we already saw, the Active property can be set to True at design or run time. We can also call the Open method at run time. For example, add the following code to the form's OnCreate event handler to get the data from the ADOTable1 component:

ADOTable1.Open;

Note: Every ADO dataset can acess data in a database through its own ConnectionString property or through an ADOConnection component (and it's ConnectionString). If the ADOTable1 component is connected to ADOConnection1 component (preferable) than opening the ADOTable will result in activating the corresponding ADOConection component. The ADOConnection provides two events that will be executed: OnWillConnect and OnConnectComplete.

The Open method sets the Active property to True and activates the connection. When we are done with using the connection we can close it by setting the Active property to False or by calling the Close method. Generally you will place the call to Close in the form's OnClose event handler:

ADOTable1.Close;

Before moving on, it's crucial to know that working with dataset's methods and properties relies on knowing the current state of the data. Simply put, the State property of a dataset determines what actions can and cannot occur at any moment on a dataset.

   How are you doing?
If the dataset is closed the State of the data indicates an Inactive connection. No operations or actions or methods can be done on the data while the connection is closed. The first time we open the connection the dataset is placed in the default Browse state. You should always be aware of the state "your" data is in. For example, when we connect a dataset to a DBGrid, the user is able to see the underlying dataset (or recordset), but to be able to change some of the data the State must be changed to Edit.

It's important to know that the dataset state constantly changes as an application processes data. If, for example, while browsing the data in a DBGrid (Browse state) the user starts editing the records the state will automatically change to Edit. Of course, this is the default behaviour of the data-aware controls (DBGrid, DBEdit) with their AutoEdit property set to True.

But, how do we get the state? The ADOTable (nor any other dataset component) doesn't have an event that triggers when the State changes.
Ok, let's see: for each dataset component we generally use one datasource component to present a link to one or more data-aware controls. That's it.

Every datasource component has an OnStateChange event that fires whenever the state of the underlying dataset changes. Placing the following code for the OnStateChange event handler causes the caption of the form to indicate the current state of the ADOTable1 dataset component:

procedure TForm1.DataSource1StateChange
  (Sender: TObject);
var ds: string;
begin
 case ADOTable1.State of
  dsInactive: ds:='Closed';
  dsBrowse  : ds:='Browsing';
  dsEdit    : ds:='Editing';
  dsInsert  : ds:='New record inserting';
 else
  ds:='Other states'
 end;
 Caption:='ADOTable1 state: ' + ds;
end;

Next page > From BOF to EOF > Page 1, 2

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming

©2009 About.com, a part of The New York Times Company.

All rights reserved.