Getting System.InvalidCastException when using OleDbDataReader.GetInt64() after fetching Long from MS Access database

I've been working on a project (with Visual Studio 2013) in which I need to retrieve information from a MS Access 2007 database stored locally. I'm using OleDb to deal with the connection at the moment. The database has a single table, with several fields. I am trying to retrieve the value from SID - which is the primary key, set to Auto-Number as a Long Integer from within Access.

Here's my problem: when the OleDbDataReader has finished executing and I try to retrieve the result (using the GetInt64 method) I get this error:

An unhandled exception of type 'System.InvalidCastException' occurred in System.Data.dll

Additional information: Specified cast is not valid.

This happens regardless of whether I'm assigning the result to a value or not. Changing the call to GetInt32 makes it work, but I've no idea why!

Could anybody shed light on why this is the case?

I've searched extensively on here and elsewhere, mostly they suggest the field type is not set to Long Integer in Access, but mine is already, so I don't think that's the issue. None of the other solutions seem to apply or work.

I've extracted the problem code from my main application, and stripped away all non-essential code, and still get the error, here is the simple version:

        // Set up connection and test query
        OleDbConnection connection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Combobulator\\Desktop\\Testing Databases\\Reagents Database\\Reagents.mdb");
        string query = "SELECT SID FROM MolTable WHERE [Chemical Name] = 'Acetyl Chloride'";

        OleDbCommand command = new OleDbCommand(query, connection);

        try {
            connection.Open();
            OleDbDataReader reader = command.ExecuteReader();

            if (!reader.HasRows) {
                System.Console.WriteLine("Returned no rows.");
                Environment.Exit(-1);
            }

            // Find all matching entries
            while (reader.Read()) {
               reader.GetInt64(0);   // This is where the error is thrown
            }

            // Close the reader and connection
            reader.Close();
        } catch (OleDbException e) {
            System.Console.WriteLine("Error: " + e.Errors[0].Message);
        } finally {
            connection.Close();
        }

I'm totally stumped, this is the first time I've had to ask online for help.

Thanks for taking the time to read!

EDIT: I forgot to mention, this isn't just me confusing the length of Long Integer in Access is it? I assume that it's 64-bit, please correct me if I'm wrong.

Jon Skeet
people
quotationmark

I forgot to mention, this isn't just me confusing the length of Long Integer in Access is it? I assume that it's 64-bit, please correct me if I'm wrong.

Yup, I think that's exactly the problem. For example, from "Field types in MS Access":

Integers in Access come in 1, 2 and 4 byte varieties. The single byte number is named Byte (Range 0-255), the two-byte number is named Integer (-32768 to 32767) and then there is the Long Integer (-2 billion to 2 billion).

(This site agrees.)

MS documentation is thin on the ground, but HansUp found this "Introduction to data types and field properties" that includes:

Field Size

  • ...
  • Long Integer — Use for integers that range from -2,147,483,648 to 2,147,483,647. Storage requirement is 4 bytes.

people

See more on this question at Stackoverflow