C# Insert ArrayList in DataRow

Go To StackoverFlow.com

1

I want to insert an arraylist in Datarow.

using this code,

      ArrayList array=new ArrayList();
      foreach (string s in array) 
      {
           valuesdata.Rows.Add(s);
      }

But My datatable must have only one datarow. My code created eight datarows. I tried,

valuesdata.Rows.Add(array);

But it doesn't work.That should be

valuesdata.Rows.Add(array[0],array[1],array[2],array[3]....);

How can I solve this problem?

Thanks.

2012-04-03 23:14
by NoName
Your array has nothing in it, so of course indexing into it will fail. Put some data in the array variable first - debracey 2012-04-03 23:21
No...This code only for example.Array isn't empty.. - NoName 2012-04-03 23:41
C# is not Java. You should be using List<string>debracey 2012-04-04 01:37


4

try this:

        ArrayList array = new ArrayList();

        String[] arrayB = new String[array.Count];
        for (int i = 0; i < array.Count; i++)
        {
            arrayB[i] = array[i].ToString();
        }

        valuesdata.Rows.Add(arrayB);
2012-04-03 23:37
by John Woo
Excellent!.A) doesn't work.But B) fine...Thnk u - NoName 2012-04-03 23:52


3

try like this:

//1 - declare the array ArrayList object
ArrayList array = new ArrayList();

//2 - here add some elements into your array object

//3 - convert the ArrayList to string array and pass it as ItemArray to Rows.Add
valuesdata.Rows.Add(array.ToArray(typeof(string)));
2012-04-03 23:35
by Davide Piras
Ads