Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have this problem that prevents me to collect a proper set of data.
I want to Foreach the items from a listbox and split the items.
The splitted items must be store in a single Array

I have this syntax.
while (reader.Read())
   {
      var tmp = reader["Items"];
      if (tmp != DBNull.Value)
      {
        ListBox1.Items.Add(tmp.ToString()); // The data that added in the Listbox is already
                                            // 1 string ("ItemA,ItemB,ItemC,ItemD")
      }
   }
   foreach (var items in ListBox1.Items)
     {
        myDb.Add(new MyItems() { string.Join(",", items) } ); 
         //Here, I want to transfer the items from listbox. 
     }


The result I got from this process is a single data in 1 array, not multiple.
Actual Result:
{"ItemA,ItemB,ItemC,ItemD"}


Expected Result:
{ "ItemA", "ItemB", "ItemC", "ItemD" }



I found this usefull:
Here[^]

and I have this syntax now.

IEnumerable<string> row;
IEnumerable<string> permutations;
     while (reader.Read())
     {
     var tmp = reader["Items"];
     if (tmp != DBNull.Value)
         {
         ListBox1.Items.Add(tmp.ToString());   
         }
     }
     row = new string[] {ListBox1.Items.ToString()};
     permutations = GetPermutations(row, delimiter: ",");
     foreach (var permutation in permutations)
     {
         myDb.Add(new MyItems() {permutation});
     }                        


Error: {System.Web.UI.WebControls.ListItemCollection}
Posted
Updated 20-Jan-14 15:17pm
v5
Comments
Dev Jonie 20-Jan-14 8:12am    
I've made a little change and a little bit clearer for the question
Dev Jonie 20-Jan-14 8:16am    
I need a set of arrays.

ex.
myDB.Add
{ "ItemA", "ItemB", "ItemC", "ItemD" }
{ "ItemA", "ItemC", "ItemE", "ItemF" }
{ "ItemA", "ItemB", "ItemC" }
{ "ItemA", "ItemB" }
Dev Jonie 20-Jan-14 21:16pm    
Guys I Updated my question and found some good hints.

Try this
C#
foreach (var items in ListBox1.Items)
           {
               myDb.Add(new MyItems() { items });
           }
 var array = myDB.ToArray();
 
Share this answer
 
Comments
Dev Jonie 20-Jan-14 8:16am    
I wan't to use myDb.Add(new MyItems() { items });
as final array.
Karthik_Mahalingam 20-Jan-14 8:24am    
can u pls post the code for this class MyItems
Karthik_Mahalingam 20-Jan-14 8:25am    
then

myDB = myDB.ToArray();
Karthik_Mahalingam 20-Jan-14 8:39am    
try this

foreach (var items in ListBox1.Items)
{
myDb.Add(items.ToString());

}
Dev Jonie 20-Jan-14 8:43am    
tried that but stores "ItemA,ItemB,ItemC,ItemD" in single index.
I fetched that data from a query as a single item. what i need is to parse it and put it according to their arrangements in a new container(list/array).
Maybe try:
C#
while (reader.Read()) {
   // Here I would like to know how you assign you 'tmp' variable?
   if (tmp != DBNull.Value) {
      ListBox1.Items.Add(tmp.ToString()); // The data that added in the Listbox is already
                                          // 1 string ("ItemA,ItemB,ItemC,ItemD")
   }
}
List<string> list = new List<string>();
foreach (var items in ListBox1.Items) {
   // myDb.Add(new MyItems() { string.Join(",", items) } ); 
   //Here, I want to transfer the items from listbox.
   list.AddRange(((string)items).Split(','));
}
// Here you have a list of single string-elements { "Item1", "Item2", "Item3", "Item4" } obtained from multiple-string elements { "Item1,Item2", "Item3,Item4" }
// Do what you have to do with them in your DB
 
Share this answer
 
v3
Comments
Dev Jonie 20-Jan-14 11:46am    
object does not contain .Split()
so
list.AddRange(items.Split(',')); - encountered the error
phil.o 20-Jan-14 11:54am    
Yep, sorry. We are sure items are of string type, so see my updated answer.
Dev Jonie 20-Jan-14 18:59pm    
I tried but not the solution i needed.

var tmp = reader["Items"];
if (tmp != DBNull.Value) {
ListBox1.Items.Add(tmp.ToString()); // The data that added in the Listbox is already
// 1 string ("ItemA,ItemB,ItemC,ItemD")
}

There you go, its an object(tmp) to call the item from db. I hope it might help you to analyze the problem
hi try this


C#
List<string> list = new List<string>();
            foreach (var item in listBox1.Items)
            {
                list.Add(item.ToString());
            }


like this you can try.
 
Share this answer
 
v2
C#
List<string> li_str = new List<string>();

foreach (var items in ListBox1.Items)
     {
        li_str.Add(items);
     }


string[] arr_str = li_str.ToArray();

</string></string>

Now you have your array ready in "arr_str".
 
Share this answer
 

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