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.

3
  • 2
    Your array has nothing in it, so of course indexing into it will fail. Put some data in the array variable first. Commented Apr 3, 2012 at 23:21
  • No...This code only for example.Array isn't empty... Commented Apr 3, 2012 at 23:41
  • C# is not Java. You should be using List<string> Commented Apr 4, 2012 at 1:37

2 Answers 2

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);
Sign up to request clarification or add additional context in comments.

Comments

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)));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.