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.
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:
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.
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 itsRecNo property to position the cursor at specific record.
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.
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:
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.
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.
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.
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.