Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8630581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:05:33+00:00 2026-06-12T09:05:33+00:00

I have a C# program that is supposed to pull data from a small,

  • 0

I have a C# program that is supposed to pull data from a small, four-column spreadsheet (.xlsx). It reads the first column fine, however when it gets to the second I get the following error:

“Specified cast is not valid”

I’ve checked and re-checked the format of the cells, and there is no difference between the first column, which reads just fine, and the second. Below are the values for the first row in the spreadsheet. Each value represents one separate column.

121220 330004 Dual 02/22/2012

And here is the code that I am using. the Id variable loads just fine, it is the courseCode which is causing the problem.

string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;"
    + "Data Source=C:\Projects\sym_AgentServices_INT\Book1.xlsx;"
    + "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
string queryString = "SELECT * FROM [CE$]";

try
{
    OleDbDataReader reader;
    using (OleDbConnection connection = new OleDbConnection(connectionString))
    {
        OleDbCommand command = new OleDbCommand(queryString, connection);
        connection.Open();
        reader = command.ExecuteReader();

        while (reader.Read())
        {
            string Id = "";
            string courseCode = "";
            string partner = "";
            string effectiveDate = "";
            string expirationDate = "";

            Id = reader.GetString(0);
            courseCode = reader.GetString(1);
            partner = reader.GetString(2);
            effectiveDate = reader.GetString(3);
            expirationDate = reader.GetString(4);
        }
    }
}

EDIT

I’ve analyzed the reader object at runtime and found the following: by expanding base a number of times and opening up the _bindings property of System.Data.OleDb.OleDbDataReader I found that there was only one entry in there (“0”). However, if I open 0 up, then expand the _columnBindings property I find the values 0 – 4.

Could my syntax be incorrect when extracting data from the reader (“reader.GetString(x)”)?

FINAL EDIT

Instead of using .GetString(x) I would highly recommend simply using reader[x].ToString(). This solved the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:05:35+00:00Added an answer on June 12, 2026 at 9:05 am

    When I use reader[n].ToString(), the error goes away for me:

            string connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=d:\temp\Book1.xlsx; Extended Properties='Excel 8.0;HDR=Yes;IMEX=1'";
            string queryString = "SELECT * FROM [CE$]";
    
            OleDbDataReader reader;
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                OleDbCommand command = new OleDbCommand(queryString, connection);
                connection.Open();
    
                DataSet ds = new DataSet("Book1");
    
                IDataAdapter adapter = new OleDbDataAdapter(queryString, connection);
                adapter.Fill(ds);
    
                reader = command.ExecuteReader();
    
                while (reader.Read())
                {
                    string Id = "";
                    string courseCode = "";
                    string partner = "";
                    string effectiveDate = "";
                    string expirationDate = "";
    
                    Id = reader[0].ToString();
                    courseCode = reader[1].ToString();
                    partner = reader[2].ToString();
                    effectiveDate = reader[3].ToString();
                    expirationDate = reader[4].ToString();
    
                    //Id = reader.GetString(0);
                    //courseCode = reader.GetString(1);
                    //partner = reader.GetString(2);
                    //effectiveDate = reader.GetString(3);
                    //expirationDate = reader.GetString(4);
                }
            }
    

    Here’s my test data set…

    ID  Course Code     Partner Effective Date Expiration Date
    100 5               MS      10/3/2012       10/3/2013
    200 21-400          Oracle  10/3/2012       10/3/2013
    300                 Goog    10/3/2012       10/3/2013
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a small program that's supposed to sample some value from a device
I have a small program that I am supposed to write that makes a
I'm writing a program that is supposed to generate four random integers (from 1
I have a program that is supposed to interact with a web server and
I have a program for the iPhone that is supposed to be doing intelligent
I have a program that gets a JSON from the server using getJSON and
I have a program that is supposed to send a file to a web
I have a C# Program that is supposed to be multi-OS compatible. It requires
I have a button in my program that supposed to be clicked after a
I have simple serial port program that is supposed to read the serial port

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.