tags:

views:

391

answers:

6

This is my code

    con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/techsoft   /PP1.accdb;Persist Security Info=False");
        con.Open();
        cm = new OleDbCommand("select aa from ab", con);
        OleDbDataReader qq;
        qq = cm.ExecuteReader();
        ArrayList ss = new ArrayList();

        while (qq.Read())
        {
            object[] values = new object[qq.FieldCount];
            qq.GetValues(values);
            ss.Add(values);



        }

if i use this syntax to convert

int[] i = (int[])ss.ToArray(System.Type.GetType("System.Int32"));

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

plz suggest me a one solution or any other alternative way

+1  A: 

As far as I can tell, the elements you are putting into ss appear to be arrays themselves.

You are then trying to cast these arrays to ints which obviously won't work.

What you should do is something like:

while (qq.Read())
{
    //object[] values = new object[qq.FieldCount]; - Necessary?
    //qq.GetValues(values); - Necessary?
    ss.Add(qq["aa"]);
}
Paolo
Thank u so much its working fine
ush
You're welcome, do feel free to mark this as the accepted answer ;)
Paolo
A: 

Don't use an ArrayList, go for List<int> and you get what you want for free (int array from a list).

If you want to reference the System.Type that represents System.Int32 I'd prefer typeof(int) without string literals and this unnecessary lookup.

The identifiers are hard to read, btw - you might want to make them more easy to understand when you ask a question.

The real problem was identified by the other posters: You add arrays to the list, which aren't integers. Maybe(?) arrays of integers, but we wouldn't know without more details.

Benjamin Podszun
A: 

If there are objects that are not integers in your ArrayList ss then you need to specify whether to fail or to ignore them.

Assuming you want to ignore them, then you can use LINQ:

int[] res = ss.OfType<int>().ToArray();

(Assuming you have a using System.Linq in scope.)

Richard
A: 

It may be that one of your values cannot be converted to an Int32. A better approach would be to use the generic List type. This behaves like the ArrayList but only accepts Int32 values. Then you can use the ToArray() method that returns a real Int32[] array.

Rune Grimstad
A: 

I'd use a strongly typed List<int> for this. Also, since you're only selecting one field from the table, I'd say your code can be simplified a bit:

List<int> myInts = newList<int>();

using(IDataReader reader = cm.ExecuteReader()) {
    while (reader.Read())
    {
        myInts.Add(int.Parse(reader["aa"].ToString()));
    }
}

Then you could just do

myInts.ToArray();
David Hedlund
A: 

If you're using at least .NET 3.5:

using System.Linq;

//...

int[] myArray = ss.Cast<int>().ToArray();

Otherwise, you have foreach through your ArrayList manually.

herzmeister der welten