Click here to Skip to main content
15,881,781 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to store ListBox multiple selected value in databse in one column seprated with comma(,). Please help me
Posted

You just read the values out of your listbox, put a comma between them and then insert them as one string in to the database, assuming that's what you need. What have you tried ?
 
Share this answer
 
Just Use This :

String get_value="";
for (int i = 0; i < ListBox1.Items.Count; i++)
            {
                if (ListBox1.Items[i].Selected)
                {
                          get_value = get_value +ListBox1.Items[i].Value+","; 
                }
            }


Now , you can have String with Comma Seprated and Perform Insert Query Using get_value String to insert that String.
 
Share this answer
 
Even though you've been given advice how to concatenate the selected values into a single string, my advice is: Don't do this.

There are several things why this kind of approach is a bad design. For example:

  • You need to write extra logic to concatenate the data and also to split it when you read the data
  • If the selected values are text, a comma would then be an illegal character in the data
  • If the values are numbers, you'll have to do unnecessary conversions
  • Modifications are more troublesome since you have to rebuild the string
  • You cannot use for example foreign keys to enforce data integrity
  • and so on...


Instead, in my opinion you should create a small table where each row represents a selection and the table would be properly using foreign keys to the actual parent table and perhaps to a lookup table (possible values for the selection).
 
Share this answer
 
Hi,

Iterate through all the value of the ListBox and join with comma. it will gives you simple string value. Then as you are inserting string in your database , you can insert this comma separated value in the database.

What you need to learn is,
1) How to Iterate through ListBox Values ?
2) How to Join string values with comma ?

Hope you find answers of above simple question, and then you can answer your question as well. Sometimes you are very near to the answer but you just have to look into it deeply. Try hard and use Google/MSDN/CP.

Best of luck

Thanks
-Amit Gajjar.
 
Share this answer
 
Read the selected texts from the List Box using a loop up to the selected items count.Append comma to the end of each string if the loop count is not equal to the selected items count.After the loop,save it to the database.Hopes it will help you.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900