RTC Forums
November 24, 2024, 12:58:03 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
 
   Home   Help Login Register  
Pages: [1]
  Print  
Author Topic: Data retrieving from server error  (Read 4916 times)
bebeantics
RTC License+
****
Posts: 29


« on: December 27, 2016, 11:52:14 PM »

Hi.

I have a server side function, something like in examples:

procedure TfrmMainMarcaje.getnewrowsExecute(Sender: TRtcConnection;
  Param: TRtcFunctionInfo; Result: TRtcValue);
var
  rtcDS: TRtcDataSet;
  qr:tfdquery;
begin
  //Create a TRtcDataSet object to store the data from our dataset
  qr:=tfdquery.Create(self);
  qr.Connection:=dt1.db;
  qr.SQL.Text:='select * from marcaje where statusmonitorizare=0 order by id limit 5';
  qr.Open();
  rtcDS := TRtcDataSet.Create;
  try
    //Activate the dataset
    //Store the result in the rtcDS object we already created
    memo1.Lines.Add('linii raspuns: '+qr.RecordCount.ToString());
    DelphiDataSetToRtc(qr, rtcDS);
    //Send back the result
    Result.asObject := rtcDS;
  except on E:EDatabaseError do begin
    //If something went wrong we free the created TRtcDataSet object
    memo1.Lines.Add('err');
    rtcDS.Free;
    end;
  end;
  rtcDS.Free;
  qr.Close;
  qr.Free;
end;


I have another event from a client:
    rtcMemDataSet.Active := false;
    with rtcClientModule do begin
    Prepare('getnewrows');
    end;
    rtcClientModule.Call(rtcResult1);

The result RtcResult1Return is like this:
if Result.isType = rtc_Dataset then
  begin
    rtcMemDataSet.asObject := Result.asDataSet;
    Result.Extract;
    rtcMemDataSet.Active := True;
  end;
   if Sender <> nil then
      PostInteractive;
  if Result.isType = rtc_Exception then
    showmessage( 'Cannot connect to the Server. Check your network and that the Server is running.');

  if rtcmemdataset.RecordCount>0 then beep;
end;

The problem is not getting any information on the client side and can not figure out what it is. Can someone help me?
The program enters the
  [if Result.isType = rtc_Dataset then] loop.

RtcHttpClient
Autoconnect=true
Useproxy=true

RtcClientModule
Autorepost=2
AutosyncEvents = true
client = Rtchttpclient
modulefilename = /query      same on server too
Logged
D.Tkalcec (RTC)
Administrator
*****
Posts: 1881


« Reply #1 on: December 28, 2016, 09:32:39 AM »

1. With Result.asObject:=rtcDS, you are assigning a pointer to the original rtcDS object to the Result instance (not a COPY) and should NOT free the rtcDS object afterwards, or you will be facing all sorts of problems, like data contained in rtcDS NOT being sent to the Client and/or Access Violations on the Server. The easiest way to ensure you won't accidentally free rtcDS (you are manually doing it in your code - two times), you should set it to NIL immediately after the assignment, like this ...

  Result.asObject:=rtcDS;
  rtcDS:=nil;

2. If you want Exceptions which you are manually handling inside your remote function code on the Server to be forwarded to the Client, you have to re-raise them (using "raise"), like this ...

  except on E:EDatabaseError do begin
    //If something went wrong we free the created TRtcDataSet object
    memo1.Lines.Add('err');
    rtcDS.Free;
    raise; // this will re-raise the exception
    end;

3. If your Server is MultiThreaded and you want to access the GUI from a remote function (for example, to access a "TMemo" component on your Form), you have to synchronize your remote function with the Main Thread. To synchronize the entire remote function with the Main Thread, you can add this like at the top of your remote function event, immediately after "begin" ...

procedure TfrmMainMarcaje.getnewrowsExecute(Sender: TRtcConnection;
  Param: TRtcFunctionInfo; Result: TRtcValue);
var
  rtcDS: TRtcDataSet;
  qr:tfdquery;
begin
  if Sender.Sync(getnewrowsExecute,Param,Result) then Exit;

4. If you receive a Result with the type rtc_Exception on the Client (Result.isType=rtc_Exception), it means that an Exception was raised inside the remote function implemented on the Server and the Exception was left unhandled or was re-raised. In that case, Result.asException will contain the Exception Message. That is NOT an indication of connection problems with the Server (see the ShowMessage line in your Client-side Result event). In case of connection problems, the RequestAborted event will be triggered on the TRtcResult component assigned to receive the result from the Server, and ... the OnResponseAbort event will be triggered on the TRtcClientModule component posting the request. These are the places where you can handle connection-related problems.

Best Regards,
Danijel Tkalcec
Logged
bebeantics
RTC License+
****
Posts: 29


« Reply #2 on: December 29, 2016, 09:28:32 PM »

Hi Danijel

Sorry for the late response but I was busy and could not work on the problem.
I have read and applied your suggestions and problem was solved. I deleted that rtcDS.Free before qr.Close, and everything is ok. I have introduced the suggestions of paragraphs 1 and 2.

Thanks for the help.
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!
Page created in 0.024 seconds with 17 queries.