Hello,
I am trying to save a list of multiple choices as rows with same ID in database once hit submit from my form, I created a store procedure to save data. Can you please help me on this?
Thank you.
Hello,
I am trying to save a list of multiple choices as rows with same ID in database once hit submit from my form, I created a store procedure to save data. Can you please help me on this?
Thank you.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Ivonne AspilcuetaPosted Feb 27, 2025, 5:18 PM
Sorry everyone who is tryng to help me, it is hard to explin by comment instead posting my code, but forum does not allow me to post ant code anymore, I guees I need points for that.
If you know a way to share my code, please let me know. Thank you so much for your help.
Shem OtienoPosted Feb 27, 2025, 9:10 AM
Right ! Ivonne,here’s a general approach to save a list of multiple choices as rows with the same ID in a database using a stored procedure.
Let's assume you have a table structure like this:
sql
And you have a list of choices to save for a specific ID. You can create a stored procedure to handle this task.
Step 1: Create the Stored Procedure
Here's an example of a stored procedure to save the data:
sql
Step 2: Call the Stored Procedure from Your Form
When you submit the form, collect the list of choices and concatenate them into a single string separated by commas. Then, call the stored procedure and pass the ID and concatenated choices string.
Example in C# (ASP.NET):
Step 3: Ensure Data Integrity
Make sure to validate the choices and handle any potential errors, such as SQL injection attacks, by using parameterized queries as shown above.
Tuhin PaulPosted Feb 26, 2025, 10:25 AM
Part - 2
Tuhin PaulPosted Feb 26, 2025, 10:24 AM
Part - 1
Let's assume you have:
A form with multiple checkboxes (e.g., user selections)
Need to store selections in a table with structure:
This procedure handles multiple choices using a comma-separated string:
Application CodeCSV Handling: Uses
STRING_SPLIT()(SQL Server 2016+) to parse comma-separated valuesTransaction Management: Ensures all inserts succeed or fail together
Error Handling: Returns detailed error messages
Scalability: Handles any number of choices
Sreenath KappoorPosted Feb 26, 2025, 5:12 AM
Hello
To avoid conversion errors, you can cast the value as INT. Also check if the value is numeric in where condition.
For example;
INSERT INTO @CHOICETABLE (CHOICEID) SELECT VALUE FROM STRING_SPLIT(@CHOICES, ',');
This query can be changed as
SELECT DISTINCT TRY_CAST(TRIM(value) AS INT)
FROM STRING_SPLIT(@Choices, ',')
WHERE ISNUMERIC(TRIM(value)) = 1
TRY_CAST - Trim spaces and convert to INT
ISNUMERIC - Only numeric values are processed
This will insert only numeric values.
Ivonne AspilcuetaPosted Feb 25, 2025, 7:18 PM
Hello,
It is giving this error:
System.Data.SqlClient.SqlException: 'Conversion failed when converting the nvarchar value 'Branch Deposit Checks' to data type int.'
Any thoughts?
Jignesh KumarPosted Feb 25, 2025, 5:03 PM
Hello,
You can create store procedure as below,
Emily FosterPosted Feb 25, 2025, 4:21 PM
Hello! It sounds like you're looking to save multiple choices as individual rows in a database table, all associated with the same ID. This can be a common requirement in many applications, especially when dealing with forms that capture multiple selections.
To achieve this, you can follow these general steps:
1. Form Submission:
When the user submits the form with the multiple choices selected, your backend (whether it be in a programming language like Python, PHP, Java, etc.) receives this data.
2. Database Interaction:
You mentioned using a stored procedure to save the data. In the stored procedure, you'll want to iterate through the list of choices and insert each choice as a separate row in the database table. Make sure you include the common ID that links all these choices together.
3. Stored Procedure Example (MySQL):
Here's a simplified example of how your stored procedure might look in MySQL to save multiple choices under the same ID:
4. Calling the Stored Procedure:
From your backend code, you would then call this stored procedure, passing in the common ID and the list of choices (which could be formatted as a comma-separated string, for example).
5. Validation and Security:
Ensure that you validate and sanitize the user input to prevent any potential security risks like SQL injection.
By following these steps and adjusting them to fit the specifics of your application and database system, you should be able to save multiple choices as individual rows with the same ID successfully. If you encounter any specific issues or need further assistance with a particular database system or programming language, feel free to provide more details!