Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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...
 
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 12-Dec-10 9:37am
LAPEC628
Comments
ManfredRBihy at 12-Dec-10 15:24pm
   
@Walt:Third line in his code!
Walt Fair, Jr. at 12-Dec-10 15:06pm
   
What are you trying to do here:
result.Add(new string[reader.FieldCount]);
Where is result defined?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
List<object[]> result = new List<Object[]>();
...
result.Add(new Object[reader.FieldCount]);
reader.GetValues(result[i]);
 
Cheers
 

Manfred
  Permalink  
v3
Comments
LAPEC at 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
ManfredRBihy at 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 at 12-Dec-10 16:07pm
   
thanks Manfred i got the problem its working now...
thanks again
 
kind regards
 
roni
Dalek Dave at 12-Dec-10 17:17pm
   
Good answer Manfred!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

If you are more comfortable dealing with DataTables than List<>s, you might consider putting your data into a DataTable using this:
    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:
 
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);
            
            }
}
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 Dec 2010
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100