Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear People

Im having a problem with array. On my form_load im executing an sql statement by using an OleDbDataReader object. Here is my code...

MSIL
public partial class DtposMDIParentSystem : Form
    {
        
        List<string[]> result = new List<string[]>();
        
        public DtposMDIParentSystem()
        {
            InitializeComponent();
        }
        
        private void DtposMDIParentSystem_Load(object sender, EventArgs e)
        {
            //create the database connection
            OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");
            
            //connection = new OleDbConnection(connectionString);
            //create the command object and store the sql query
            OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);
            try
            {
                aConnection.Open();
                //create the datareader object to connect to table
                OleDbDataReader reader = aCommand.ExecuteReader();
                int i = 0;
                while (reader.Read())
                {
                    result.Add(new string[reader.FieldCount]);
                    reader.GetValues(result[i]);
                }
                reader.Close();
                aConnection.Close();

            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Invalid Masseage = " + ex.Message);
            
            }
}


When I run my application I get a message saying

ArrayTypeMismatchException was unhandled

Attempted to access an element as a type incompatible with the array.


Any help would be very very greatfull...

thanks in advance


Kind regards

Roni
Posted
Comments
Dr.Walt Fair, PE 12-Dec-10 15:06pm    
What are you trying to do here:
result.Add(new string[reader.FieldCount]);
Where is result defined?
Manfred Rudolf Bihy 12-Dec-10 15:24pm    
@Walt:Third line in his code!

Hi Lapec,

your problem is in the declaration of result:

List<string[]>


and this line here:

reader.GetValues(result[i])


The reader is trying to store something into the string array that is most likely not a string. What ever is returned from the DB not all columns are of string type. You might have success by turning all of the columns of table food into strings, but that might not work for the DB your using.

Try to go like this: List<object[]> result = ...
Then later on when you access what was read you'd have to detect the type and make an explicit cast to access whatever is there.


Modification:
C#
List<object[]> result = new List<Object[]>();
...
result.Add(new Object[reader.FieldCount]);
reader.GetValues(result[i]);


Cheers


Manfred
 
Share this answer
 
v3
Comments
LAPEC 12-Dec-10 15:36pm    
Dear ManfredRBihy

thanks for responding to my question, but as a matter of fackt is still not working its giving me the same error.

Could you please explain the a bit of code more in detail.
thanks again in advance

kind regards

roni
Manfred Rudolf Bihy 12-Dec-10 15:52pm    
I'm sorry Roni. I've overlooked that: result.Add(new stringObject[reader.FieldCount]);
The array that get's added has to be of type Object and not string. That is why you were getting the same error. I'll adjust my answer.
LAPEC 12-Dec-10 16:07pm    
thanks Manfred i got the problem its working now...
thanks again

kind regards

roni
Dalek Dave 12-Dec-10 17:17pm    
Good answer Manfred!
If you are more comfortable dealing with DataTables than List<>s, you might consider putting your data into a DataTable using this:
C#
public class DBUtilities
{
    public static DataTable ReaderToTable(DbDataReader reader)
    {
        DataTable newTable = new DataTable();
        DataColumn col;
        DataRow row;
        int i;

        for (i = 0; i < reader.FieldCount; i++)
        {
            col = new DataColumn();
            col.ColumnName = reader.GetName(i);
            col.DataType = reader.GetFieldType(i);

            newTable.Columns.Add(col);
        }

        while (reader.Read())
        {
            row = newTable.NewRow();
            for (i = 0; i < reader.FieldCount; i++)
            {
                row[i] = reader[i];
            }

            newTable.Rows.Add(row);
        }

        return newTable;
    }
}


you would use it in your code like this:

C#
public partial class DtposMDIParentSystem : Form
    {
        
        DataTable result = null;
        
        public DtposMDIParentSystem()
        {
            InitializeComponent();
        }
        
        private void DtposMDIParentSystem_Load(object sender, EventArgs e)
        {
            //create the database connection
            OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");
            
            //connection = new OleDbConnection(connectionString);
            //create the command object and store the sql query
            OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);
            try
            {
                aConnection.Open();
                //create the datareader object to connect to table
                OleDbDataReader reader = aCommand.ExecuteReader();
                result = DBUtilities.ReaderToTable(reader);

                reader.Close();
                aConnection.Close();

            }
            catch (InvalidOperationException ex)
            {
                MessageBox.Show("Invalid Masseage = " + ex.Message);
            
            }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900