TableView Refresh

  1. 5 years ago

    TheMexican

    24 Jun 2018 User since 2016

    How do you refresh a TableView connected to a MySQL Database to reflect newly added data?
    I have a Window (Patients) with a TableView connected to a MySQL Database. Window Loads up with a list of patients.

    I have another window where user can enter new Patients. When I enter a new Patient it is saved on the database with no problems, but the Window with the TableView will not show the new entry.

    I have to close Simulator an when I re-open it shows the new entry. See attached screenshot.

    I have tried TableView.refresh() and also running the query again to no avail.
    Thanks!!

  2. marco

    24 Jun 2018 Administrator User since 2016

    Hi @TheMexican according to TableView official documentation https://docs.creolabs.com/classes/TableView.html you should use the reload method (passing true as boolean parameter to force DataSet reload too).

  3. TheMexican

    24 Jun 2018 User since 2016

    I tried the Tableview reload method and it still doesn't show the new entry.
    I have query (Recordset) that performs the Insert into the Mysql.
    On the DidFinish event of the recordset I put the following:
    wndPatients.tblPatients.reload(true); //This is the main Window that shows all of the patients

    When I go back to the Patients table, the new entry doesn't show. But it is in the database.
    If you want I can send project since it has no real data.

  4. andrea

    25 Jun 2018 Administrator User since 2016
    Edited 5 years ago by andrea

    Hi @TheMexican,
    query (Recordset) objects are intended for executing queries (SELECT instructions) on a database, using a query to perform an INSERT can lead to unexpected results as in your case. In your case it may really updates your db but the returned recordset is null so the query fires a DidFail event instead of the DidFinish event where you put the reload code.

    The recommended way modify a DB is the following (please see the attached sample project ReloadTable.creoproject, it uses a SQLite DB but you can do the same with a MySQL DB):

    1. Add a custom method to your Database object (in my example: addRow(name)) and use the execute method of the DB to perform an INSERT instruction, you can add arguments to the custom method to specify dynamic values for your SQL instruction (in the example I use the argument name ). As the second argument of the execute method I set the onSuccess code that performs the reload of the table, for example:

    var onSuccess = { 
    	Window1.TableView1.reload(true); 
    }
    
    var onError = { 
    	Console.write("Error")
    }
     
    SQLite1.execute("INSERT INTO Table1 (Column1) values ('\(name)')", onSuccess, onError)

    reloadTable_image1.png

    2. You can then use that method when you need to add a new value, in my example I use the following code in the Action event of the AddButton:

    SQLite1.addRow(TextField1.text)

    P.S.: Another way to reload the table after the execution of the INSERT instruction, instead of Window1.TableView1.reload(true);, can be to re-run the Query (in my example SQLite1.Query.run();). After the execution of a query (Recordset), all the controls that use the recordset as the configured Dataset will be reloaded. I use this way in the DeleteButton in the attached example.

  5. TheMexican

    1 Jul 2018 User since 2016

    @andrea , I did as indicated and I am having the same issues as before, now the execute statement won't add the new Patient and the table will not refresh.
    I am attaching my project so you can take a look.

    This is a temporary MariaDB database with a temporary password with fake data, so I don't mind anybody seeing it.

    Please advise as to what I am doing wrong?

  6. andrea

    3 Jul 2018 Administrator User since 2016

    @TheMexican,
    The gblDB.execute was not working because the gblDB was not connected, but it was not executing the onError closure because of a bug, I've implemented a fix that will be released in the next update of Creo. The execute method of a DB doesn't automatically connect the DB (only the run method of a RecordSet performs a connect if not yet connected).
    So I added the following connection code to your AddPatient method:

    if (!gblDB.isConnected) {
    	var onConnect = {
    		Console.write("Connected \(gblDB.isConnected)")
    		gblDB.AddPatient()
    	}
    	var onConnectError = {
    		Console.write("Can't connect")
    	}
    	gblDB.connect(onConnect, onConnectError)
    	return
    }

    I also added the following code to the WillShow event of the wndPatients:

    wndPatients.tblPatients.reload(false);

    The table was not reloaded by calling wndPatients.DBRooms.qryPatientsInitial.run(); because the AddPatient method is executed when the wndPatients is not shown, by calling the reload method in the WillShow event assure you to refresh the table with the new data of the qryPatientsInitial.

    In my previous example ReloadTable.creoproject the connect code was not needed because the global DB object was used both for the Query (used as the dataset of the TableView) and for the execute method, so when the execute method was called the DB was already connected.

    I've attached the modified version of your project.

  7. TheMexican

    7 Jul 2018 User since 2016

    Thank you, I am learning more and more :)

or Sign Up to reply!