Home arrow Delphi-Kylix arrow Page 4 - Using the Client Dataset in Two-Tiered Client/Server Applications
DELPHI-KYLIX

Using the Client Dataset in Two-Tiered Client/Server Applications


Client-server based applications have a number of advantages over file-based applications. To make such an approach work, however, the client dataset requires a provider component. This article will introduce you to the dataset provider component.

Author Info:
By: Danish Ahmed
Rating: 5 stars5 stars5 stars5 stars5 stars / 5
December 26, 2007
TABLE OF CONTENTS:
  1. · Using the Client Dataset in Two-Tiered Client/Server Applications
  2. · Two-Tiered Architecture
  3. · Using Data-Aware Components with the Client Dataset
  4. · Navigating and Modifying Data Programatically

print this article
SEARCH DEVARTICLES

Using the Client Dataset in Two-Tiered Client/Server Applications - Navigating and Modifying Data Programatically
(Page 4 of 4 )

If for some reason you do not want to use the DBNavigator component and instead wish to write code to move the cursor back and forth and also manipulate records, you can do so by calling the methods of TClientDataset component. The following are some methods:


CDS1.First;

CDS1.Next;

CDS1.Prior;

CDS1.Last;

 

You don't always have to move the cursor through the records sequentially. The "MoveBy" method allows you to specify the number of rows you want to move forward or backward.


 

CDS1.MoveBy(3);

CDS1.MoveBy(-3);


If the cursor encounters the End or Beginning while moving forward or backward, the cursor is placed at the last or first record and does not move the number of rows specified in the method. In the above instance, if the total number of records is 10 and the cursor is currently at 8, it will not move 3 rows forward since it encounters the end. Instead, the cursor will be placed at the last record. Similarly, if the cursor were at the second record, passing a -3 value would not move it backwards by 3 records but would place it at the first record.

While working with filtered records, the methods for positioning the cursor at specific records varies a little. To move to the first record that matches the criteria, the method required is FindFirst and not simply First as in an unfiltered dataset. The following methods can be used to navigate a filtered dataset.


CDS1.FindFirst; //Searches for the first matching record and makes it current record.

CDS1.FindNext; // Moves to the next field matching the criteria

CDS1.FindPrior; // Moves the cursor to the previous record in the filtered dataset.

CDS1.FindLast; // Moves to the last matching record in the filtered dataset.


If no matching records are found, the position of the cursor remains unchanged. 

Another property of the client dataset which can be useful for navigation is the RecNo property. Unlike other datasets, the client dataset allows you to use its RecNo property to position the cursor at specific record.


CDS1.RecNo:=4;



To insert a new record, modify or delete an existing record, you can call the member methods Insert, Edit, Save and Delete of the client dataset.


CDS1.Insert;

CDS1.Edit;

CDS1.Delete;

CDS1.Post;


When calling any of these methods, the client dataset is put in a particular state. For instance when the Insert method is called, the dataset is put in the dsInsert state, and after the call to the Edit method it is put in the dsEdit state. While working with standard controls, the dataset states can be used to determine a change. For example, if you have a button labeled "save changes," you can set its enable property to true only when the dataset is in the dsEdit state.

To work directly with the values held by the fields in the dataset you can call the call methods depending on the situation. The simplest method for accessing field values would be to retrieve the value by calling Value method:


Edit1.Text:=fName.Value;


In the above code, the Edit1 component is assigned the value held by the "fName" field. However, if the data returned is not of the string type you would be required to convert it before assigning it to Edit1. To access the field value as variant you can call the FieldValues method with the field name as the parameter.


Edit1.Text:=CDS1.FieldValues['fName'];


In the above method you can also use the index number of the record in place of field names. This can be pretty useful when you are not aware of the field names. But if you know the names of the fields you can instead use the FieldByName method which is much faster than the other methods. You can also use the field object's conversion property in this method itself to avoid any type conversion errors during runtime.

 

Edit.Text:= CDS1.FieldByName('fName').AsString; // read value to edit box


CDS1.FieldByName('fName').AsString:=Edit1.Text; // write value from edit box



If you are inserting multiple records, especially by iterating through a loop, there is a probability of an exception being raised when the cursor is at the end of the records. Use the EOF and BOF properties to determine if the cursor is at the end of the dataset or at its beginning. When the cursor is located at the first row of the dataset the BOF property returns true and if it is located at the last row of the dataset, the EOF property returns true. The EOF property can be used as a loop condition to control iteration of all records in the dataset.


try

........

CDS1.First;

while not CDS1.Eof do

begin

ComboBox1.Items.Add(CDS1.FieldByName('fName').AsString);

CDS1.Next;

end;


finally

.....


In the above code, the cursor is placed at the first row by the CDS1.First method , while statement checks if the cursor is not at the last row of the dataset and processes the next block of statements. BOF can be used in conditional iterations or conditional statements in a similar manner but in a different situation.

In this article we discussed how the client dataset and provider can be used to boost performance of a two-tier application. The next part of this series will be focused on n-tier architecture.

Danish Ahmed

http://www.mindfiresolutions.com/


DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

blog comments powered by Disqus
DELPHI-KYLIX ARTICLES

- Loading an XML Document into the DOM
- Delphi Wrapper Classes and XML
- Delphi and the DOM
- Delphi and XML
- Internet Access: Client Service
- Finishing the Client for an Internet Access ...
- The Client for an Internet Access Control Ap...
- User Management for an Internet Access Contr...
- Important Procedures for an Internet Access ...
- Server Code for an Internet Access Control A...
- Constructing the Interface for an Internet A...
- Building a Server Application for an Interne...
- Building an Internet Access Control Applicat...
- Client Dataset: Working with Data Packets an...
- Using the Client Dataset in an N-Tiered Appl...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials