//
// LoadTVPResource
//
procedure TfrmMain.LoadTVPResource(
var ADatastore: TVpBufDSDatastore; AJSON: string);
var
intGridRow, intNumberOfSQLQueryRows: integer;
// JSON related variables
jsParser: TJSONParser;
jsArrayRow: TJSONArray;
jsObj: TJSONObject;
//
lastRes: TVpResource;
begin
//
if AJSON = EmptyStr then
exit;
//
ADataStore.CreateTables;
// create the json parser
jsParser := TJSONParser.Create(AJSON);
//
try
// Parse the JSON data
jsArrayRow := jsParser.Parse as TJSONArray;
//
try
// get the number of rows returned by the query in the JSON data object
intNumberOfSQLQueryRows := jsArrayRow.Count;
// Empty the dataset
ADataStore.ResourceTable.Close;
ADataStore.ResourceTable.Open;
//AComboBox.Clear;
// append the json rows to the grid
for intGridRow := 0 to Pred(intNumberOfSQLQueryRows) { - 1} do
begin
// assign the object to jsObj
jsObj := jsArrayRow.Objects[intGridRow];
//
try
// All events related to a particular resource
//
with ADataStore.ResourceTable do
begin
//
Append;
//
if not jsObj.Items[0].IsNull then
begin
FieldByName('resourceid').AsInteger := jsObj.Items[0].AsInteger;
ShowMessage(jsObj.Items[0].AsString);
ShowMessage(FieldByName('resourceid').AsString);
end
else
FieldByName('resourceid').Clear;
//
if not jsObj.Items[2].IsNull then
FieldByName('description').AsString := jsObj.Items[2].AsString
else
FieldByName('description').Clear;
//
if not jsObj.Items[3].IsNull then
FieldByName('notes').AsString := jsObj.Items[3].AsString
else
FieldByName('notes').Clear;
//
if not jsObj.Items[4].IsNull then
FieldByName('imageindex').AsInteger := jsObj.Items[4].AsInteger
else
FieldByName('imageindex').Clear;
//
if not jsObj.Items[5].IsNull then
FieldByName('resourceactive').AsBoolean := jsObj.Items[5].AsBoolean
else
FieldByName('resourceactive').Clear;
//
Post;
end;
finally
//
jsObj.Clear;
end;
end; // for intGridRow := 0 to intNumberOfSQLQueryRows - 1 do
finally
//
jsArrayRow.Free;
end; // try...finally (jsParser := TJSONParser.Create(AJSString))
finally
// free the jsParser
FreeAndNil(jsParser);
end;
//
ADataStore.ResourceTable.Active := True;
// Connect the datastore to the newly added data
if not ADatastore.Connected then ADatastore.Connected := True;
//
if ADatastore.Resources.Count > 0 then
begin
// Find the last resource ID value
lastRes := ADatastore.Resources.Items[ADatastore.Resources.Count];
ADatastore.ResourceID := lastRes.ResourceID; [b]<----------------- ResourceID is set to 1 here[/b]
end;
end;