# Insert and update row using button click in power table

**URL:** <https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955>\
**Category:** Ignition\
**Tags:** ignition81\
**Created:** [March 13, 2023, 9:20am UTC](https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955 "2023-03-13T09:20:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [March 13, 2023, 9:20am UTC](https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955/1 "2023-03-13T09:20:06Z")

</div>

Hi Guys,

I have tried to update and insert the row using a button click. When I write any value in the cell and go to the next cell or button click then automatically deleted newValue. and insert new row Null and does update newValue.

Any idea for how to set a new value and insert, and update a row using a button click?

1. Below script in button action performed for insert row:

> 

```auto
table = event.source.parent.getComponent('tbl')
> row = table.selectedRow   
> if system.gui.confirm("insert?"): 
> Nummer = event.source.parent.parent.Nummer
> col1 = table.data.getValueAt(row, "col1")
> col2 = table.data.getValueAt(row, "col2")
> col3 = table.data.getValueAt(row, "col3")
> 	
> args = [Nummer, col1,col2,col3]
> 	
> insertRecord = system.db.runPrepUpdate("insert into Test (Nummer, col1,col2,col3) values (?,?,?,?)", args) 
> if insertRecord > 0:
> system.db.refresh(table, "data")

```

1. Below script to update a row in the button action performed:

```auto
table = event.source.parent.getComponent('tbl')
data = event.source.parent.getComponent('tbl').data   
row = event.source.parent.getComponent('tbl').selectedRow  

ValueOne = data.getValueAt(row, "col1")
ValueTwo = data.getValueAt(row, "col2")
ValueThree = data.getValueAt(row, "col3")
ValueFour = data.getValueAt(row, "ID") 
args = [ValueOne,ValueTwo,ValueThree,ValueFour]

if system.gui.confirm("are you sure?"): 
	UpdateRecord = system.db.runPrepUpdate("UPDATE Test SET col1 = ?, col2=?,col3=? where ID = ? ", args) 
	system.db.refresh(table, "data")
	if UpdateRecord > 0:
		system.gui.messageBox("updated.")

```

Thanks in advance  
Priyanka

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [March 13, 2023, 9:37am UTC](https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955/2 "2023-03-13T09:37:46Z")

</div>

It sounds like you are not refreshing the power table binding after updating/inserting into the database. The code you are looking for is `system.db.refresh(powerTable, 'data')`  
[https://docs.inductiveautomation.com/display/DOC81/system.db.refresh](https://docs.inductiveautomation.com/display/DOC81/system.db.refresh)

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 13, 2023, 9:38am UTC](https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955/3 "2023-03-13T09:38:44Z")

</div>

> [@justinedwards.jle](#):
>
> The code you are looking for is `system.db.refresh(powerTable, data)`

Pssst!

`system.db.refresh(powerTable, 'data')`

---

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [March 13, 2023, 9:55am UTC](https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955/4 "2023-03-13T09:55:02Z")

</div>

@justinedwards.jle @pturmel Thanks for the quick response.

But I have already added a refresh function after the runPrepUpdate function.

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [March 13, 2023, 10:01am UTC](https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955/5 "2023-03-13T10:01:12Z")

</div>

Please edit your original post to properly mark your scripts as "preformatted text" so they are readable. Please always do this for posted scripts and error reports.

---

<div class="post-metadata">

**Author:** ![Priyanka.Khandge](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/priyanka.khandge/32/35989_2.png) [@Priyanka.Khandge](https://forum.inductiveautomation.com/u/Priyanka.Khandge)\
**Post date:** [March 13, 2023, 10:20am UTC](https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955/6 "2023-03-13T10:20:11Z")

</div>

The issue I am facing right now is whenever I enter a new value into a particular cell the cell is not getting reflected with that value, it shows a blank cell.

Can you please help with this?

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [March 13, 2023, 10:25am UTC](https://forum.inductiveautomation.com/t/insert-and-update-row-using-button-click-in-power-table/70955/7 "2023-03-13T10:25:49Z")

</div>

Use this in your onCellEdited script to change your table's dataset:  
[https://docs.inductiveautomation.com/display/DOC81/system.dataset.setValue](https://docs.inductiveautomation.com/display/DOC81/system.dataset.setValue)

It is possible that you will have to remove your binding, and script the dataset population to do what you are doing.
