1

Possible Duplicate:
Convert ArrayList into string array(string[]) in c#

How can I convert array list to string[]?below is code I am trying. I am getting an error:

At least one element in the source array could not be cast down to the destination array type.

ArrayList myArrayList = new ArrayList();
foreach (DataRow dtRow in DtSet.Tables[0].Rows)
{
    myArrayList.Add(dtRow);
}

string[] myStringArray = (string[])myArrayList.ToArray(typeof(string));
6
  • 2
    Are you still using .NET 1.1. or why don't you use typed List<T> instead of an ArrayList? Commented Jul 16, 2012 at 9:24
  • 2
    "At least one element in the source array could not be cast down to the destination array type." it really is telling you the problem here... kind-of; every "element in the source array" is a DataRow. It is telling you it can't cast a DataRow to a string. Which is entirely correct; a DataRow is not a string... Commented Jul 16, 2012 at 9:32
  • You are trying to cast DataRows into strings. You need to first check if that is permissible with the rows that you have. If it is, you can simply loop through and case them individually and add it to string array. But I doubt this cast would succeed. Commented Jul 16, 2012 at 9:33
  • 1
    @Shaks: DataRows are never strings and can never be casted to string meaningfully. DateRow.ToString() would call Object.ToString() which is implemented as this.GetType().ToString(). What you can do is cast every field in every datarow to string and join these fields. Commented Jul 16, 2012 at 11:36
  • @TimSchmelter - I guess that's what is needed. :) Commented Jul 17, 2012 at 12:17

7 Answers 7

5

Are you still using .NET 1.1. or why don't you use typed List<T> instead of an ArrayList?

First, you cannot cast a DataRow to String. You can cast every field of a DataRow to a string.

If you can use Linq you can use following code to get a String[] of all row fields where each field is separated by comma:

String[] rowFields = DtSet.Tables[0]
                    .AsEnumerable()
                    .Select(r => string.Join(",", r.ItemArray));
                    .ToArray();
Sign up to request clarification or add additional context in comments.

Comments

2

You cannot take a DataRow (type) and add it to an ArrayList expecting it to be a string. A DataRow is an object that has a myriad of information about the object as well as the objects data.

foreach(DataRow dtRow in dtSet.Tables[0].Rows)
{
    myArrayList.Add(dtRow["UserName"].ToString());
}

string[] myArray = myArrayList.ToArray(typeof(string));

Comments

2

You are placing a number of DataRow objects into an ArrayList. DataRows are not strings. So: that can't work. At best, you can perhaps get the .ToString(), but frankly, that isn't going to be all that helpful - because every row will just say System.Data.DataRow (since it doesn't override ToString(). I think you need to re-evaluate what you are trying to do.

Example based on comment discussion:

DataTable table = ...
string[] strings = new string[table.Rows.Count];
int idx = 0;
foreach(DataRow row in table.Rows)
{
    StringBuilder sb = new StringBuilder();
    object[] cells = row.ItemArray;
    for(int i = 0 ; i < cells.Length ; i++)
    {
        if (i != 0) sb.Append(',');
        sb.Append('"').Append(cells[i]).Append('"');
    }
    strings[idx++] = sb.ToString();
}

5 Comments

i have one dataset and i need to pass values as string array[] to one method,how can i do that?converting dataset to string[]?
@user1527989 well, what do you expect a DataRow to look like as a string? For example, a DataRow with 5 cells, {1,"abc",2.0,true,-12} - what should that look like as a string?
@user1527989 but what string? what would it be? I've given you a row of sample data: how would you want that to appear, as a string?
@user1527989 k; next step: write some code that does that. See edit for example.
1

You have an ArrayList whose elements are of type DataRow. You are trying to convert it into an array of strings. A DataRow is not a string (nor does it become one on downcasting), so you can't do that.

If the array of strings is what you ultimately want, you should probably call ToString (or whatever conversion you need) on each element while filling the ArrayList.

(Also, as Tim Schmelter said in comments, you should really be using a List<T> for some T -- probably either string or DataRow, depending on exactly how your code ends up looking -- rather than an ArrayList.)

Comments

1

Each item the arrayList is of type DataRow, you can't cast it to a string.

Instead, you should construct a string that represents the contents of the row, and add that string to the list, e.g.

myArrayList.Add(dtRow["CustomerName"]);

Comments

0

Have you tried this:

string[] array = myArrayList.ToArray(typeof(string)) as string[];

4 Comments

That will error in exactly the same way. ArrayList already returns a right-typed array, when it completes the ToArray method - which isn't happening here.
@user1527989 10 minutes ago, myself, Gareth, Dane etc told you pretty clearly that what you are trying to do cannot work, and exactly why. Why are you still trying to do that, rather than address the fundamental issue: a DataRow is not a string ?
My bad, should have read the question fully. @Marc is correct, you're adding DataRows to your arraylist and then trying to cast them to string which is not possible. You need to rethink your logic.
humm ok gareth.i got the problem.thanks
0
ArrayList myArrayList = new ArrayList();

myArrayList.Add("as");
myArrayList.Add("as");
myArrayList.Add("as");
myArrayList.Add("as");
myArrayList.Add("as");            
myArrayList.Add("as");

string[] array = myArrayList.ToArray(typeof(string)) as string[];
foreach (var item in array)
{
    Console.WriteLine(item.ToString());                
}

Output:

as
as
as
as
as
as

1 Comment

While this is accurate as a standalone example, it does not address the context of the question, which specifically concerns DataRow.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.