GetInt32 cast problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pieps86
    New Member
    • Aug 2009
    • 6

    GetInt32 cast problem

    Hello,
    I am querying an oracle database to get the min and max of a field, and I'm trying to do it as efficiently as possible for my C# app. The code I have is as such:

    command.Command Text="SELECT MIN(row_id), MAX(row_id) from my_table t where row_id is not null";
    OracleDataReade r reader=command. ExecuteReader() ;
    int min=reader.GetO rdinal("min(row _id)");
    int max=reader.GetO rdinal("min(row _id)");
    reader.Read();
    callnumber=read er.GetInt32(min );
    maxnumber=reade r.GetInt32(max) ;

    However, I keep getting the following error when trying to set callnumber.

    System.InvalidC astException: Specified cast is not valid..


    callnumber and maxnumber are just standard C# ints, declared as class variables. Any help is appreciated.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I would say the reader is not returning variables of type int32. Perhaps they are UInt32?
    I have never seen a reader used like that with the GetOrdinal calls.
    Why does GetOrdinal use the "min(row_id )" and GetInt32 uses just "min"?

    Also, I'm sure it was just a copy/paste typo, but int max is also looking at the min value

    Comment

    • pieps86
      New Member
      • Aug 2009
      • 6

      #3
      GetOrdinal uses min(row_id) because that is the name of the column that is returned. I am using just the int 'min' with GetInt32 because that is the corresponding integer that reader.GetOrdin al("min(row_id) ") returns.

      I noticed that int max looks at the min just after I posted it :)

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Ok for some reason I thought it was "min" not min.
        What if you throw this line in there:
        MessageBox.Show (reader.GetSqlV alue(min).GetTy pe().ToString() );
        See what data type its trying to return?

        Comment

        Working...