Show Selection-based Client Data

Navigation:  Tutorial: The Form Editor > Sample creation of a form >

Show Selection-based Client Data

Version 1.0.0

After having listed all clients in a ListBox, a few data of the client from the database are now to be loaded and displayed by selecting them in the ListBox. The data to be loaded are now permanently defined in regard to the computer domain, computer description, IP address, MAC address, the operating system name, operating system version and the current ServicePack.

 

For the above-mentioned data, you add a label and an edit box on the form for each data set. . You can name the labels in line with the data set that is to be displayed in the matching edit field. Likewise enter eloquent names for the edit fields with the help of the Object Inspector, which can be activated by means of the fields. In the example, these are the names EditDomain, EditDescription, EditIP, EditMAC, EditOS, EditVersion, EditSP.

 

To be able to enter the data of a client selected from the ListBox in the components you have just createdm the ListBox must be adjusted. To customize the ListBox, click on it and select the Events tab in the Object Inspector. Then double-click on the empty box next to the OnClick event. Thus, the ListBox1Click () function is automatically created in the source code, which is activated automatically when you select a client in the ListBox.

 

11.3.2 - Formeditor

Add more elements to show details

 

First, you now have to declare the variables LSqlID, LSqlPC und LSqlOS as a string as well as LID, LPC and LOS as TStringList. Furthermore, the strings pc and os and the required integer pos are required. Then the variables LID, LPC and LOS have to be initialized with TStringList.Create.

 

The data of the selected client are now read into a try/finally block. For this purpose, the LSqlID variable is set with a SQL statement, which reads the ClientID from the database. The ClientID is identified here by means of the client name of the selected client. The currently selected client name can be found out via listBox1.items.strings [listBox1.ItemIndex].

 

After the SQL statement LSqlID has been executed with sqlquery, the result is contained in the TStringList variable LID. However, since the result is back again in the format column name = value, the actual value is read using a copy and then saved again in the same variable.

 

Since the ClientID of the selected client is now known, the variables LSqlPC and LSqlOS can each be linked to one SQL statement. In this case, the SQL statement of the variables LSqlPC reads the domain, description, IP address and MAC address data from the database table CLT_CLIENTS. The SQL statement of the variables LSqlOS, on the other hand, reads the ​​operating system, version and service pack values from the database table CLT_OPERATINGSYSTEM. By means of the Where clause in conjunction with the Client ID read immediately before, only the data of a single client are read.

 

The results of the sqlquery queries with variables LSqlPC and LSqlOS are contained in the variables LPC and LOS. To be able to further process and correcltly display the results, the result strings are saved in the matching pc or os variables.

 

The results are now available in the format column name=value,column name=value, [...]. These results are then segmented front to back with the delete, pos and copy functions. As a result, the actual values ​​are copied from the result and entered with EditX.Text directly into the corresponding edit field. This is first performed for the pc variable, and subsequently for the os variable.

 

In the final step, you should release the LID, LPC and LOS variables again with .Free.

 

If you now start the form and select a client, its values ​​should be displayed.

 

procedure ListBox1Click(Sender: TObject);

var

 LSqlID, LSqlPC, LSqlOS: String;

 LID, LPC, LOS: TStringList;

 pc, os : String;

 pos: integer;

begin

 LID := TStringList.Create;

 LPC := TStringList.Create;

 LOS := TStringList.Create;

 

 try

           LSqlID := 'Select CLIENTID from CLT_Clients WHERE COMPUTERNAME='''

                    + listBox1.items.strings[listBox1.ItemIndex] + '''';

 

      SqlQuery(LSqlID, '', '', LID, true);

 

      if (LID.Count > 0) then

     begin

           LID.strings[0] := Copy(LID.strings[0], 10, Length(LID.strings[0]));

 

           LSqlPC := 'Select COMPUTERDOMAIN, COMPUTERDESCRIPTION, PRIMARYIP, MACADDRESS' 

                          + ' From CLT_CLIENTS Where CLIENTID=' + quotedstr(LID.strings[0]);

 

           LSqlOS := 'Select OSSTRING, PRODUCTTYPE, SERVICEPACK From CLT_OPERATINGSYSTEM' 

                         + ' Where CLIENTID=' + quotedstr(LID.strings[0]);

 

           SqlQuery(LSqlPC, '', '', LPC, true); pc := LPC.strings[0];

           SqlQuery(LSqlOS, '', '', LOS, true); os := LOS.Strings[0];

 

           Delete(pc, 1, 15); pos := Pos(',', pc);

           EditDomain.Text := Copy(pc, 0, pos-1);

           pos := Pos('=', pc); Delete(pc, 1, pos); pos := Pos(',', pc);

           EditDescription.Text := Copy(pc, 0, pos-1);

           pos := Pos('=', pc); Delete(pc, 1, pos); pos := Pos(',', pc);

           EditIP.Text := Copy(pc, 0, pos-1);

           pos := Pos('=', pc); Delete(pc, 1, pos);

           EditMAC.Text := Copy(pc, 0, Length(pc));

 

           Delete(os, 1, 9); pos := Pos(',', os);

           EditOS.Text := Copy(os, 0, pos-1);

           pos := Pos('=', os); Delete(os, 1, pos); pos := Pos(',', os);

           EditVersion.Text := Copy(os, 0, pos-1);

           pos := Pos('=', os); Delete(os, 1, pos);

           EditSP.Text := Copy(os, 0, Length(os));

      end;

 

 finally

              LID.Free;

           LPC.Free;

           LOS.Free;

 end;

end;

Last change on 10.03.2014