I am using OLEDB is only reading the first 11 rows of the excel sheet

650 views Asked by At

I am having an issue with a data reader object that seems to only be reading the first 11 rows from an excel spreadsheet. I have tried updating the OLEDB driver from JET to ACE and have also tried adding a .HasRows clause to my .Read() loop. Nothing seems to work. Same result every time.

This Arraylist populates an object created to hold all of the data from the excel file

public ArrayList LoadCensusFromExcelFile( string fPath)
{
System.Data.OleDb.OleDbConnection conn = new OleDbConnection() ;
conn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + `
`fPath + ";Extended Properties=\"Excel 8.0;HDR=Yes;\";" ;
try
{
conn.Open() ;
}
        catch (OleDbException ex)   
        {   
            conn.Close();   
            conn.Dispose();   
            throw (ex);   
        } 
            catch( System.Exception openFailed )
        {
            _errorMessage = "Could not open workbook: " + openFailed.ToString() ;
            Log.LogMessage(openFailed,"Census Excel Upload Failed {0}","Connect String: " + conn.ConnectionString + "Message: " + openFailed.ToString());
            return null ;
        }
            ArrayList census = new ArrayList() ;

        try
        {
            census = _loadCensus(conn) ;
        }
        catch( System.Exception e )
        {
            _errorMessage = "Uncaught CensusImport._loadCensus() exception: " + e.ToString() ;
            Log.LogMessage(e,"Census Excel Upload Failed {0}",e.ToString());
            return null ;
        }

//this is the declaration of the reader object and the query of the excel sheet

    private ArrayList _loadCensus( OleDbConnection conn )
    {
        System.Data.OleDb.OleDbDataReader rdr = null ;

        try
        {
            OleDbCommand cmd = new OleDbCommand( "select * from Census", conn ) ;
            rdr = cmd.ExecuteReader() ;
        }
        catch ( System.Exception e )
        {
            _errorMessage = "Could not execute select statement: " + e.ToString() ;
            Log.LogMessage(e,"Census Excel Upload Failed {0}",e.ToString());
            return null ;
        }

        return _loadCensusData( rdr ) ;
    }

Finally, this ArrayList populates all of the data from the excel sheet. it loops though until it gets to the 11th row and the starts returning NULL values.

rdr2String is a custom method that converts all of the indexed values in the reader object into strings.

private ArrayList _loadCensusData( OleDbDataReader rdr )
    {

        ArrayList censusList = new ArrayList() ;
        CensusVO census ;

        string FirstName ;
        string LastName ;
        string[] BirthDate = new string[2];
        string AppAge ;
        string AppGender ;

        while (rdr.Read())
        {
            census = new CensusVO() ;

            FirstName       = rdr2String(rdr,0,50) ;
            LastName        = rdr2String(rdr,1,50) ;
            BirthDate[0]    = rdr2String(rdr,2,30) ;
            AppAge          = rdr2String(rdr,3,30) ;
            AppGender       = rdr2String(rdr,4,1) ;


            censusList.Add(census);
           }

            return censusList ;
}
0

There are 0 answers