Connecting Tech Pros Worldwide Forums | Help | Site Map

GetInt32 cast problem

Newbie
 
Join Date: Aug 2009
Posts: 6
#1: Aug 10 '09
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.CommandText="SELECT MIN(row_id), MAX(row_id) from my_table t where row_id is not null";
OracleDataReader reader=command.ExecuteReader();
int min=reader.GetOrdinal("min(row_id)");
int max=reader.GetOrdinal("min(row_id)");
reader.Read();
callnumber=reader.GetInt32(min);
maxnumber=reader.GetInt32(max);

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

System.InvalidCastException: Specified cast is not valid..


callnumber and maxnumber are just standard C# ints, declared as class variables. Any help is appreciated.

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#2: Aug 11 '09

re: GetInt32 cast problem


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
Newbie
 
Join Date: Aug 2009
Posts: 6
#3: Aug 13 '09

re: GetInt32 cast problem


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.GetOrdinal("min(row_id)") returns.

I noticed that int max looks at the min just after I posted it :)
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#4: Aug 13 '09

re: GetInt32 cast problem


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