Icon View Thread

The following is the text of the current message along with any replies.
Messages 1 to 10 of 19 total
Thread Can't save random number to database
Mon, Dec 1 2014 4:36 PMPermanent Link

TD

Advanced Data Systems, Inc.

I have a form where I create a random number.  I then assign this random number to a TEdit which is connected  to a dataset.  When I save the record everything gets written to the database table except the random number.  I am using ElevateDB as the database engine and the field, that the random number is being written to, has the data type set to float.  What am I doing wrong?

Thanks,
TD

Here is a screen capture of the form and the code behind this form:

unit Unit6;

interface

uses WebCore, WebForms, WebCtrls, WebData;

type

  TForm6 = class(TForm)
     Edit1: TEdit;
     Label1: TLabel;
     Label2: TLabel;
     Edit2: TEdit;
     Label3: TLabel;
     Edit3: TEdit;
     Edit4: TEdit;
     Button1: TButton;
     ComboBox1: TComboBox;
     Label5: TLabel;
     ds6: TDataSet;
     Label4: TLabel;
     Label6: TLabel;
     procedure Button1Click(Sender: TObject);
     procedure Form6Show(Sender: TObject);
     procedure ds6AfterLoad(Sender: TObject);
  private
     { Private declarations }
  public
     { Public declarations }
  end;

var
  Form6: TForm6;

implementation

uses unit1;

var
  dblRandom: double;
  strRandom: string;
  intRandom: integer;

procedure TForm6.Button1Click(Sender: TObject);
begin
  Database.StartTransaction;
     try
        Form6.ds6.Save;
        Database.Commit;
        Form1.LoadDateRange(false);
     except
        Database.Rollback;
        raise;
     end;
  Form6.Close;
end;

procedure TForm6.Form6Show(Sender: TObject);
begin
  Database.Load(ds6);
end;

procedure TForm6.ds6AfterLoad(Sender: TObject);
begin
  Form6.ds6.Insert;
  dblRandom := Random;
  strRandom := DoubleToStr(dblRandom);
  //strRandom := StrReplace(strRandom,'0.','',true,true);
  intRandom := StrToInt(strRandom);
  Form6.Edit1.SetFocus;
  Form6.Edit1.Text := strRandom;
  //Form6.ds6.columns['random_number'].AsFloat := dblRandom;
end;

end.



Attachments: Form6.JPG
Mon, Dec 1 2014 5:14 PMPermanent Link

Walter Matte

Tactical Business Corporation

This is what I do.

You need to StartTransaction - Before Dataset.Insert....


Database.StartTransaction;
DataSet.Insert(False)
DataSet.Columns['xxx'].As integer := myRandom;
DataSet.Save;



In Dataset.OnSaveEvent

Database.Commit;


Walter
Mon, Dec 1 2014 5:59 PMPermanent Link

Walter Matte

Tactical Business Corporation

You should associate OnCommit event - before the Database.Commit;  (and OnCommitError)

Then in the OnCommit do the

Database.Load(XXXX);

Walter
Mon, Dec 1 2014 6:15 PMPermanent Link

Walter Matte

Tactical Business Corporation


I have attached a Data Entry Example.

Walter



Attachments: DatasetExample.zip
Mon, Dec 1 2014 9:40 PMPermanent Link

TD

Advanced Data Systems, Inc.

Walter Matte wrote:


I have attached a Data Entry Example.

Walter

Thank you Walter, I will take a look at this tomorrow.

Thanks again!
TD
Fri, Dec 19 2014 10:23 PMPermanent Link

TD

Advanced Data Systems, Inc.

TD wrote:

Walter Matte wrote:


I have attached a Data Entry Example.

Walter

Thank you Walter, I will take a look at this tomorrow.

Thanks again!
TD

That solved that problem, now I have a problem implementing the next part.  

Right after the record is saved I need to use a second dataset that uses a select query to find the record I just saved by using the random number in the WHERE clause.   I would then be able to have the primary key field's value that I could past to the next form that will appear as this form closes.  I goal is to create a wizard using several forms instead of having all of the fields on one form.  The wizard will be used for adding a new record as well as editing an existing record.

My problem I think is a timing issue as the query never finds and record.

Below is the code on the form that opens when adding a new record.  Also T attached a screen capture of the query in ds6B.

Any help with this is greatly appreciated!
TD

<code>

unit Unit6;

interface

uses WebCore, WebForms, WebCtrls, WebData;

type

  TForm6 = class(TForm)
     Edit1: TEdit;
     Label1: TLabel;
     Label2: TLabel;
     Edit2: TEdit;
     Label3: TLabel;
     Edit3: TEdit;
     Edit4: TEdit;
     Button1: TButton;
     ComboBox1: TComboBox;
     Label5: TLabel;
     ds6: TDataSet;
     Label6: TLabel;
     ds6B: TDataSet;
     procedure Button1Click(Sender: TObject);
     procedure Form6Show(Sender: TObject);
     procedure ds6AfterLoad(Sender: TObject);
     procedure Form6Close(Sender: TObject);
     procedure ds6BAfterLoad(Sender: TObject);
  private
     { Private declarations }
  public
     { Public declarations }
     dblRandom: double; //public field
  end;

var
  Form6: TForm6;

implementation

uses unit1, unit7;

procedure TForm6.Button1Click(Sender: TObject);
var
  strRandom: string;
begin
  if Database.InTransaction then
     try
        Form6.ds6.columns['random_number'].AsFloat := dblRandom;
        Form6.ds6.Save;
        Database.Commit;

        //find pid of record just saved
        strRandom := DoubleToStr(Form6.dblRandom);
        ds6B.params.clear;
        ds6B.params.add('rannum='+strRandom+'');
        Database.Load(ds6B);
        Form1.LoadDateRange(false);
     except
        Database.Rollback;
        raise;
     end;
  Form6.Close;
end;

procedure TForm6.Form6Show(Sender: TObject);
begin
  Database.Load(ds6);
end;

procedure TForm6.ds6AfterLoad(Sender: TObject);
begin
  dblRandom := Random;
  Database.StartTransaction;
  Form6.ds6.Insert;
end;

procedure TForm6.Form6Close(Sender: TObject);
begin
  Form7.ShowModal;
end;

procedure TForm6.ds6BAfterLoad(Sender: TObject);
var
  strMyPID: string;
begin
  strMyPID := ds6B.columns['pid'].AsString;
  Form1.strPID := strMyPID; //pid saved to public field on main form where Form7 can access it
end;

end.

<code>



Attachments: ds6B.JPG
Sat, Dec 20 2014 7:14 AMPermanent Link

Uli Becker

Generally be aware that everything in JS is async. That's why you always
have to make sure that a value was saved before you use it.

In your case you could use the OnCommit event. Since the event is called
after *every* commit, you can set a flag to find the one you are
searching for.

Uli
Sat, Dec 20 2014 8:59 AMPermanent Link

Walter Matte

Tactical Business Corporation

Just as Uli wrote:

Everything is in JS is async.



procedure TForm6.Button1Click(Sender: TObject);
var
 strRandom: string;
begin
 if Database.InTransaction then
    try
       Form6.ds6.columns['random_number'].AsFloat := dblRandom;
       Form6.ds6.Save;
       Database.Commit;

       //find pid of record just saved
       strRandom := DoubleToStr(Form6.dblRandom);
       ds6B.params.clear;
       ds6B.params.add('rannum='+strRandom+'');
       Database.Load(ds6B);
       Form1.LoadDateRange(false);
    except
       Database.Rollback;
       raise;
    end;
 Form6.Close;
end;


You need to think about what is happening at the Browser and what is happening at the Server.

When you call Database.Commit the Browser is not behave like a desktop program.  It does not wait for the Commit to finish before doing the next command.  It immediately does the next command.  You do not know how many seconds or milliseconds it will take to do the trip to the server and back. That is why it continues, and you will get the response back in an event.   You only know the Commit is finished when the event OnCommit is fired or it fails when the OnCommitError is fired.

The you can ask for the Database.Load. in the OnCommit event.

Also the Rollback logic needs a change of thinking too.  Basically when you issue a StartTransaction EWB (the browser) starts to track and remember all your browser side dataset actions.  If you were to give your user a button that says Cancel - after a StartTransaction was begun, then in that button event you would do a Rollback - to tell the Datasets that you have no intention of sending the dataset changes to the server for processing.

Working on Web Applications requires a paradigm shift - the program flow and events when interacting between Browser and Server are different than a Desktop Client Server database application.

Also the EWB Database and Datasets are browser side objects.  Don't look at them as commands to manipulate the database directly.  When you issue a StartTransaction, Commit and Rollback in EWB you are not issuing commands to a database program (EDB, DBISAM for example).  You are working on the local data storage objects (variables) in the browser, which mange data for you (based on EWB Framework) - which then get sent as JSON requests back to the Server when you ask for a Database.Commit.  So a Database Commit is really just creating a JSON object and sending it over the internet to the server.

Walter
Sat, Dec 20 2014 10:24 AMPermanent Link

TD

Advanced Data Systems, Inc.

Walter Matte wrote:

Just as Uli wrote:

Everything is in JS is async.



procedure TForm6.Button1Click(Sender: TObject);
var
 strRandom: string;
begin
 if Database.InTransaction then
    try
       Form6.ds6.columns['random_number'].AsFloat := dblRandom;
       Form6.ds6.Save;
       Database.Commit;

       //find pid of record just saved
       strRandom := DoubleToStr(Form6.dblRandom);
       ds6B.params.clear;
       ds6B.params.add('rannum='+strRandom+'');
       Database.Load(ds6B);
       Form1.LoadDateRange(false);
    except
       Database.Rollback;
       raise;
    end;
 Form6.Close;
end;


You need to think about what is happening at the Browser and what is happening at the Server.

When you call Database.Commit the Browser is not behave like a desktop program.  It does not wait for the Commit to finish before doing the next command.  It immediately does the next command.  You do not know how many seconds or milliseconds it will take to do the trip to the server and back. That is why it continues, and you will get the response back in an event.   You only know the Commit is finished when the event OnCommit is fired or it fails when the OnCommitError is fired.

The you can ask for the Database.Load. in the OnCommit event.

Also the Rollback logic needs a change of thinking too.  Basically when you issue a StartTransaction EWB (the browser) starts to track and remember all your browser side dataset actions.  If you were to give your user a button that says Cancel - after a StartTransaction was begun, then in that button event you would do a Rollback - to tell the Datasets that you have no intention of sending the dataset changes to the server for processing.

Working on Web Applications requires a paradigm shift - the program flow and events when interacting between Browser and Server are different than a Desktop Client Server database application.

Also the EWB Database and Datasets are browser side objects.  Don't look at them as commands to manipulate the database directly.  When you issue a StartTransaction, Commit and Rollback in EWB you are not issuing commands to a database program (EDB, DBISAM for example).  You are working on the local data storage objects (variables) in the browser, which mange data for you (based on EWB Framework) - which then get sent as JSON requests back to the Server when you ask for a Database.Commit.  So a Database Commit is really just creating a JSON object and sending it over the internet to the server.

Walter

Uli, Walter thank you so much for helping!  I have searched the EWB manual and this forum for information on the OnCommit event and have found nothing.  Can you guys give me an example as to how to use / work with this event?

Thanks,
TD
Sat, Dec 20 2014 11:59 AMPermanent Link

Raul

Globestar Systems

Team Elevate Team Elevate

On 12/20/2014 10:24 AM, TD wrote:
> Uli, Walter thank you so much for helping!  I have searched the EWB manual and this forum for information on the OnCommit event and have found nothing.  Can you guys give me an example as to how to use / work with this event?

I don't believe there is a OnCommit event - i assume what they meant was
that you call "Commit" and then wait for either AfterCommit or
OnCommitError events.

I suggest you review the datasets section :

http://www.elevatesoft.com/manual?action=topics&id=ewb1&section=using_datasets

It really does cover all the details on how this works. Start with
DataSet Architecture and at least read Transactions (though i'd suggest
you read them all). For example you need to be aware of the
TransactionLevel : only if it reaches 0 does the data get posted to back
end server, etc.

Raul
Page 1 of 2Next Page
Jump to Page:  1 2
Image