1

I have searched far and wide, but most Datareader problem/answer pairs concern getting past the first row, not returning anything, getting single values using datareader, etc.. Nothing quite like waht I'm encountering at the moment.

To be clear, this is an assignment for my evening class, albeit just a very small part of it.

the function takes size as int; the table has two colmuns: col1 and col2 of which col1 holds the index value as double and col2 holds a randomly generated double. table is an excel worksheet in a workbook, don't know if that's relevant.

table has been populated using an insert statement carried out by an ADO command object without problems.

Now, instead of supplying me with the amount of rows as specified by size/@size in the query (as it plays the double role of index/UID in this case), the datareader object fetches seemingly random amounts of rows. I say seemingly, because the number does seem to be fixed to the value of "size" (eg. size = 10 -> datareader contains 3 rows after .executeReader(); size = 2 -> datareader contains 113 rows; size = 5 -> 446 rows).

While debugging I kept track of @size parameter for the query remains 10.0 and I can't put my finger on when/why reader.Read() turns False.

I also substituted the parameter in the query string with a literal (5.0); which resulted in a type mismatch in criteria expression exception. But it's all Doubles, or am I missing something?! I'm guessing this is going to be the kicker somehow, but I'm at a loss right now.

As you can probably guess I'm pretty new at programming, so bear with me please.

What causes my code to behave the way it does?

private Double[] getSelection(int size, string table)
    {
        List<Double> list = new List<Double>();
        Double[] toSort;

        OleDbConnection connect = new OleDbConnection(cntstring);
        connect.Open();
        OleDbCommand command = connect.CreateCommand();
        command.CommandType = CommandType.Text;
        command.Parameters.Add("@size", OleDbType.Double).Value = Convert.ToDouble(size);
        command.CommandText = String.Format("SELECT * FROM [{0}$] WHERE col1 < @size;", table);

        try
        {
            OleDbDataReader reader = command.ExecuteReader();
            Double outputReader;
            while (reader.Read())             
            {                                 
                outputReader = Convert.ToDouble(reader.GetValue(1));  /for some reason (which is not my main concern at the moment) the reader.getDouble() method returned an invalid cast exception
                list.Add(outputReader);
            }

            toSort = new double[list.Count()];
            foreach (double d in list)
            {
                toSort[list.IndexOf(d)] = d;
            }
            string output = String.Format("{0},{1},{2},{3},{4},{5},{6},{7},{8},{9}", toSort[0], toSort[1], toSort[2], toSort[3], toSort[4], toSort[5], toSort[6], toSort[7], toSort[8], toSort[9]);
            //to check for values; the String.Format is where i first encountered the index out of bounds exception
            MessageBox.Show(output);
            reader.Close();
            reader.Dispose();
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

        }
        finally
        {
            connect.Close();
            connect.Dispose();
            return toSort;
        }
    }
Gerald V.
  • 41
  • 5
  • Can you show your connection string? I would like to check your IMEX value. And what about checking if your Excel column is really formatted as double and not as string? – Steve Feb 19 '15 at 22:25
  • I read that excel stores numbers as doubles by default, hence the double Type used. connection string : @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\******\Documents\Visual Studio 2013\Projects\prog3Les2EigenMakelijADO\prog3Les2EigenMakelijADO\EigenMakelij.xlsx; Extended Properties = 'Excel 12.0 Xml';" No IMEX value as you can see. Is that mandatory? – Gerald V. Feb 19 '15 at 22:30
  • It is sore point in reading Excel data via OleDb. [See this comments on www.connectionstrings.com](https://www.connectionstrings.com/microsoft-jet-ole-db-4-0/) – Steve Feb 19 '15 at 22:47
  • In the mean time: added HDR=YES; IMEX=1 to extended properties and surrounded path with single quotes > same results. – Gerald V. Feb 19 '15 at 22:47
  • Or read here http://stackoverflow.com/questions/10102149/what-is-imex-in-the-oledb-connection-string – Steve Feb 19 '15 at 22:48
  • thanks for those. For others, if they ever land on this question, I also found this very informative [at codeproject](http://www.codeproject.com/Articles/37055/Working-with-MS-Excel-xls-xlsx-Using-MDAC-and-Oled), although i haven't ben able to make it work yet.The type isn't respected when INSERT ... to sheet is performed, wrting text to sheet, regardless of my setup of constring, wrecking the SELECT criterium later on. But this connectionstring thing is tickling my OCD. when I have some more free time i'm going to dig deeper. When I find it I'll post – Gerald V. Feb 21 '15 at 01:26

1 Answers1

0

Did you try single quotes around @size in your select statement, i.e.

'@size'
Wignu
  • 77
  • 1
  • 1
  • 11