Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
XML
List<object> arr = new List<object>();
            foreach (Object item in ds.Tables[0].Columns)
            {
                arr.Add(item);
            }


don't know whether it returns all the column values of a table or not ,if yes then how to take these values in array of int ,or string
Posted
Updated 7-Sep-13 23:20pm
v2

1 solution

I don't think that does what you want, or expect.

That code runs through the Columns of a DataTable - so the objects it adds to your List are DataColumns, which aren;t a lot of use on their own, because they do not contain values read from the database, but descriptions of what type of values the column contains.
Probably what you want are the Rows instead, or more likely the values from the rows.
Try this:
C#
DataTable dt = ds.Tables[0];
foreach (DataRow row in dt.Rows)
   {
   foreach (DataColumn col in dt.Columns)
      {
      Console.Write("{0} ", row[col]);
      }
   Console.WriteLine();
   }
And you should see what I mean.

BTW: it is a poor idea to create a List<object> at all - the whole idea of using a List<T> instead of the outdated ArrayList is that it is strongly typed - it will not accept "any old value" but expects only the specific type you create it for. This means that many faults can be found at compile time, instead of waiting until the user finds them at run time!
 
Share this answer
 
Comments
varun150 8-Sep-13 10:58am    
i like this site its response time is great
OriginalGriff 8-Sep-13 11:40am    
Glad you like it - we do too! :laugh:
RedDk 8-Sep-13 14:21pm    
... compared to WHAT?
varun150 8-Sep-13 14:33pm    
i found nothing better than this

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