0

I have populated a DataTable from a stored procedure in an older web application written in

C# under .NET 2.0 / Visual Studio 2005.

I'm trying to populate a List with the values in the DataTable, but I keep running up against a couple issues.

My conversion process looks like this:

List<int> SpecialVendorList = new List<int>();

foreach (DataRow datarow in GetAllSpecialVendors().Rows)
{
//Loop through each row
foreach (DataColumn column in GetAllSpecialVendors().Columns)
   {
       SpecialVendorList.Add(column["ChildVendorId"]);
       SpecialVendorList.Add(column["ParentVendorId"]);
   }
}

which gives me the following error:

Can not apply indexing with [] to an expression of type 'System.Data.DataColumn'

for each of the SpecialVendorList.Add() methods.

Jon Mitten
  • 1,965
  • 4
  • 25
  • 53

2 Answers2

3

Seems like you're trying to get column values for each row. You only the first foreach loop:

List<int> SpecialVendorList = new List<int>();

try
{
    foreach (DataRow datarow in GetAllSpecialVendors().Rows)
    {
        //Loop through each row
       SpecialVendorList.Add(Convert.ToInt32(datarow["ChildVendorId"]));
       SpecialVendorList.Add(Convert.ToInt32(datarow["ParentVendorId"]));
    }
}
catch(FormatException fe)
{
    //handle the error
}

The string index here will get that column's value in that specific row

Nick Rolando
  • 25,879
  • 13
  • 79
  • 119
  • I only the first `foreach` loop? – Jon Mitten Aug 30 '11 at 22:21
  • I need both columns' values in the SpecialVendorList – Jon Mitten Aug 30 '11 at 22:22
  • @Jon yes, you on need the one foreach loop. He's adding two items to the list on each loop by explicitly naming both columns. – Jay Riggs Aug 30 '11 at 22:23
  • With the code above, I get the following errors for each .Add() method: `The best overloaded method match for 'System.Collections.Generic.List.Add(int)' has some invalid arguments` and `Argument '1': cannot convert from 'object' to 'int'` – Jon Mitten Aug 30 '11 at 22:24
  • @Jon, datarow["xxx"] is an object but your List needs int; you'll need to do the cast via a int.TryParse. – Jay Riggs Aug 30 '11 at 22:24
  • Yes. Given "ChildVendorId" and "ParentVendorId" are your column names, this will pull each of the two values from each row and store them in the `List` – Nick Rolando Aug 30 '11 at 22:24
  • @Jay As long as they are stored as an integer type in his DB, you don't need to convert it – Nick Rolando Aug 30 '11 at 22:25
  • @Shredder Is that true if the DataRow is untyped? He may also not be pulling from a DB. – Jay Riggs Aug 30 '11 at 22:26
  • @Jon I edited my answer. This will convert the values to `int` and should get rid of those errors – Nick Rolando Aug 30 '11 at 22:28
  • @Jay Riggs, how would I try Parse? – Jon Mitten Aug 30 '11 at 22:31
  • I was refering to the [int.TryParse method](http://msdn.microsoft.com/en-us/library/f02979c7(v=VS.80).aspx), which allows you to attempt to convert a string into an int without throwing an error if it fails. @Shredder's answer is correct though (+1 from me); the only thing I'd change is to use `TryParse` instead of `Convert.ToInt32` if you think there's _any_ chance that your DataTable might hold data that can't be converted to an int. – Jay Riggs Aug 30 '11 at 22:38
  • @Jay For sure, I added try/catch in there. – Nick Rolando Aug 30 '11 at 23:02
  • @Jon I added try/catch statement in my answer to help you handle any run-time conversion errors that Jay is speaking of – Nick Rolando Aug 30 '11 at 23:03
  • Ah, there's no way to retrieve anything but integers from the stored procedure, so the try / catch is unnecessary, isn't it? – Jon Mitten Aug 30 '11 at 23:44
  • @Jon Definitely not. Its good to have for *just in case* moments and to make sure your program won't error out; makes it more solid and its good practice :) – Nick Rolando Aug 31 '11 at 00:09
1

you need to add the actual values from the rows using the column as the index:

List<int> SpecialVendorList = new List<int>();

foreach (DataRow datarow in GetAllSpecialVendors().Rows)
{
   //Loop through each row
   foreach (DataColumn column in GetAllSpecialVendors().Columns)
   {
      int val;
      if (int.TryParse(datarow[column].ToString(), out val))
         SpecialVendorList.Add(val);
   }
}
Jaime
  • 6,736
  • 1
  • 26
  • 42
  • With the above code, I get the following 2 errors (same as @Shredder 's code) `The best overloaded method match for 'System.Collections.Generic.List.Add(int)' has some invalid arguments` and `Argument '1': cannot convert from 'object' to 'int'` – Jon Mitten Aug 30 '11 at 22:30
  • you're right, you'd need to parse it as ints to get the correct data into the list... fixed the sample. – Jaime Aug 30 '11 at 22:41