Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
How to store ListBox multiple selected value in databse in one column seprated with comma(,). Please help me
Posted 25-Aug-12 1:51am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 ?
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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).
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
  Permalink  
v2

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



Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 25 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100