# Sending dropdown data to a database

**URL:** <https://forum.inductiveautomation.com/t/sending-dropdown-data-to-a-database/89713>\
**Category:** Ignition\
**Created:** [April 30, 2024, 3:21pm UTC](https://forum.inductiveautomation.com/t/sending-dropdown-data-to-a-database/89713 "2024-04-30T15:21:55Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nathaniel\_Webb](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nathaniel\_Webb](https://forum.inductiveautomation.com/u/Nathaniel_Webb)\
**Post date:** [April 30, 2024, 3:21pm UTC](https://forum.inductiveautomation.com/t/sending-dropdown-data-to-a-database/89713/1 "2024-04-30T15:21:56Z")

</div>

hello, I am looking to select a reason from a dropdown, then after pressing a button, the dropdown will send to mysql. I current am using a event handler script on the button. It is not functional. What would y'all advise?

The main goal is to have a machine go down for 15 minutes, then trigger a popup where the user has to select a reason the machine went down. The data will then be stored to a database.

```python
def runAction(self, event):
	# Get the selected reason from the dropdown
	selected_reason = event.source.parent.getComponent('Dropdown').selectedValue
	
	# Check if a reason is selected
	if selected_reason:
	    try:
	        # Attempt to insert the selected reason into the database
	        system.db.runPrepUpdate("INSERT INTO ignition.senntech_201_downtime_reasons (reason) VALUES (?)", [selected_reason])
	        # Provide feedback to the user that the data was successfully stored
	        system.gui.messageBox("Data successfully stored in the database.", "Success")
	    except Exception as e:
	        # Handle any errors that occur during the database write process
	        system.gui.messageBox("An error occurred: " + str(e), "Error")
	else:
	    # If no reason is selected, display a message to the user
	    system.gui.messageBox("Please select a reason before confirming.", "Warning")

```

the actual databse name is MySQL\_Production, but the schema is ignition

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Webb](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nathaniel\_Webb](https://forum.inductiveautomation.com/u/Nathaniel_Webb)\
**Post date:** [April 30, 2024, 3:24pm UTC](https://forum.inductiveautomation.com/t/sending-dropdown-data-to-a-database/89713/2 "2024-04-30T15:24:02Z")

</div>

add the database name before address and got this error:Error running action 'component.onActionPerformed' on Machine Downtime windows/Senntech 201 Popup@D/root/Button: Traceback (most recent call last): File "function:runAction", line 3, in runAction AttributeError: 'com.inductiveautomation.ignition.common.script.ada' object has no attribute 'source'

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 30, 2024, 3:36pm UTC](https://forum.inductiveautomation.com/t/sending-dropdown-data-to-a-database/89713/3 "2024-04-30T15:36:32Z")

</div>

I am guessing you are trying to use Vision scoped things in Perspective. That won't work.

Use the property button in the script dialog to browse to the property you want.  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/6/c/6cd7a26455c67ba945ea8be43a75995a57ce3f78.png)

system.gui.\* functions are also Vision scoped. use [system.perspective.openPopup()](https://docs.inductiveautomation.com/docs/8.1/appendix/scripting-functions/system-perspective/system-perspective-openPopup) instead.

---

<div class="post-metadata">

**Author:** ![Nathaniel\_Webb](https://avatars.discourse-cdn.com/v4/letter/n/e8c25b/32.png) [@Nathaniel\_Webb](https://forum.inductiveautomation.com/u/Nathaniel_Webb)\
**Post date:** [April 30, 2024, 5:09pm UTC](https://forum.inductiveautomation.com/t/sending-dropdown-data-to-a-database/89713/4 "2024-04-30T17:09:06Z")

</div>

Is this closer?

```python
# Retrieve the selected reason from the Dropdown component within the same parent container
selected_reason = self.parent.getComponent('Dropdown').props.value

# Specify the database schema
database_schema = "ignition"

# Specify the table name
table_name = "senntech_201_downtime_reasons"

# Specify the column name for the reason
column_name = "reason"

# Check if a reason is selected
if selected_reason:
    try:
        # Insert the selected reason into the database
        system.db.runPrepUpdate("INSERT INTO {}.{} ({}) VALUES (?)".format(database_schema, table_name, column_name), [selected_reason])
        # Provide feedback to the user that the data was successfully stored
        system.perspective.print("Data successfully stored in the database.")
    except Exception as e:
        # Handle any errors that occur during the database write process
        system.perspective.print("An error occurred: " + str(e))
else:
    # If no reason is selected, display a warning message to the user
    system.perspective.print("Please select a reason before confirming.")

```
