Wednesday, March 28, 2012

How to insert all the values from a listbox into the database?

I want that the user can chose several options from one 'listbox', and to do this, I have created two 'listbox', in the first one there are the options to select, and the second one is empty. Then, in order to select the options I want the user have select one or more options from the first 'listbox' and then click in a link to pass the options selected to the second 'listbox'. Thus, the valid options selected will be the text and values in the second 'listbox'. I have seen this system in some websites, and I think it is very clear.

Well, my question is, Is it possible to insert into the database all the values from a 'listbox' control? In my case from the second 'listbox' with all the values passed from the first one? If yes, in my database table I have to create one column (field) for every possible selected option?

Thank you,
Cesarwhat you should do is have a relational table that only holds, lets say, an ID for the user and a field for the selections that have been made.|||I don' t understand what you mean. If I have a table with two fields, 'User_id' and 'product_quality_num', the 'product_quality_num' field has a foreign key related with the 'Product_Q' table. So, the user only can enter the product_quality_num 1, 2, 3, 4, 5 or 6. Then, what happens if the user have selected product_quality_num 1, 3 and 6? The 'product_quality_num' field only accepts one number.. not three (1, 3 and 6)|||You have to get those values back to the server. There have been posts on how to pass back contents of a listbox that were added client sde, but one way you can do it is by adding the IDs to a hidden form field separated by a delimiter like a pipe while you're adding them to the dropdown. The dropdown really is just a visual. The hidden form field is what you really need.

Your hidden field winds up like 1|2|4| when you post back. On the server, grab that value, parse it into an array based on the pipe. Loop through the array calling an insert for each value. Or, you could pass back the string and parse it in a SQL function that does the same thing, but returns a table as its output.

No comments:

Post a Comment