Accessing Databases (Client side)

The Client

This is the article for the client in the Accessing Databases article. At this moment, we have our Server ready, so now it’s time to create the Client that will ask for the data. In this case, this client is really simple, in fact, as simple as it could be. This Client will call a function on the Server and get back the content of the table tbl_products in our Server’s database.

Steps.

  1. Create a form for the Client with the needed components.
  2. Write the code to  call the Remote Function.

1. Create a form for  the Client with the needed components.

First, we create a new project and define a form for the client:

Client Form

Client Form

In this form, we have the next components:

  • A TLabel (lbServerAddress) and TEdit (edServerAddress).
  • A TLabel (lbServerPort) and a TEdit (edServerPort).
  • A TButton (bnConnect)
  • A TDBGrid (gdProducts) that wll be used to show the returned records.
    • Set gdProducts.DataSource = dsDataSet
  • A TRtcHttpClient (rtcClient) to make the connection to the server.
    • Set rtcClient.ReconnectOn.ConnectLost = True
    • Set rtcClient.AutoConnect = True
  • A TRtcClientModule (rtcClientModule)
    • Set rtcClientModule.ModuleFileName = /QUERY
    • Set rtcClientModule.AutoRepost = 2
  • A TRtcResult (rtcResult)
  • A TRtcMemDataSet (rtcMemDataSet). This is where we will store the dataset sent back by the Server.
  • A TDataSource (dsDataSource) to link data to the visual components.

That’s the visual part. Now we will put some code in the form.

First, we define the rtcResult Return procedure:


procedure TfmClient.rtcResultReturn(Sender: TRtcConnection; Data,
Result: TRtcValue);
begin
  //Check that the result is a DataSet
  if Result.isType = rtc_Dataset then
  begin
    //Take the result and put it in the rtcMemDataSet component
    rtcMemDataSet.asObject := Result.asDataSet;
    Result.Extract;
    //Activate and connect the DataSet to the grid
    rtcMemDataSet.Active := True;
    dsDataSource.DataSet := rtcMemDataSet;
  end
  else
    { We should not use modal dialogs from the context of connection objects.
      We have to exit from the connection object context
      by using PostInteractive. }
    if Sender <> nil then
      PostInteractive;

  { Now we are out of the connection context and can safely call a
    modal dialog. }
  if Result.isType = rtc_Exception then
    Application.MessageBox('Cannot connect to the Server. Check your network and that the Server is running.',
    'Cannot Connect', MB_OK + MB_ICONSTOP + MB_TOPMOST);

  end;

First, we check the type of the data returned by the server, it must be of type rtc_Dataset, then we take the result and store it in the rtcMemDataSet component, then we activate the rtcMemDataset component and link it to the dsDataSource component to make it available for the grid. if something goes wrong then we should get an exception of type rtc_Exception, if this is he case we will inform to the user. In order to send the message, we must be off from the connection context, so we use PostInteractive and then send the message.

Now, we define the rtcClientConnectError procedure


procedure TfmClient.rtcClientConnectError(Sender: TRtcConnection;
E: Exception);
begin
  Application.MessageBox('Cannot connect to the Server. Check your network and that the Server is running.',
  'Cannot Connect', MB_OK + MB_ICONSTOP + MB_TOPMOST);
end;

This code will be executed if there’s a problem connecting to the Server. We are just sending a message.

Now, the last part, we define the OnClick event for the bnConnect button:


procedure TfmClient.bnConnectClick(Sender: TObject);
begin
  // Set Client Connection Properties
  rtcClient.ServerAddr := edServerAddress.Text;
  rtcClient.ServerPort := edServerPort.Text;
  // Disconnect the Datasource from the Dataset
  dsDataSource.DataSet := nil;
  rtcClientModule.Prepare('GetProductsData');
  rtcClientModule.Call(rtcResult);
end;

Here we first set the Server’s Address and Port from the values entered by the user, then we disconnect the rtcMemDataSet from the dsDataSource component, then, we disconnect the rtcMemDataSet from the dsDataSource, prepare the Call to the Remote Function and then call the result from rtcResult.

And that’s it, now we just compile and run the project:

Client Running

Client Running

If there’s a problem connecting with the Server we will get an error:

Client Error

Client Error

Otherwise, we will get the results back:

Client Result

Client Result

With this simple steps we have a Server sending content over the network to a client that displays the results. This is a ver basic example but can give you a very clear idea on how powerful and easy to use are the RealThinClient components.

Files in this post:

  1. PDF of this post.
  2. Source code for the Client.

One thought on “Accessing Databases (Client side)

  1. Pingback: RealThinClient SDK – Accessing Databases (Main Article) | RealThinClassroom

Comments are closed.