DataReader.GetInt.....

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

How can I get value of a SQL column defined as int.

I try
int myInt = dataReader.GetInt32(0);

I get invalid cast exception and same for
GetInt16...

whats the best way?

Thnx
 
the cast error means the column is not a numeric column or null. if its
a string then you can parse it.

int i = int.Parse(datareader.GetString(0);

-- bruce (sqlwork.com)
 
mavrick_101 said:
Hi,

How can I get value of a SQL column defined as int.

I try
int myInt = dataReader.GetInt32(0);

I get invalid cast exception and same for
GetInt16...

whats the best way?

Thnx

As bruce said, the value is not an int. How do the query look like that
gets the value?

If the value may be null, you have to check that before trying to read
the value:

int myInt;
if (dataReader.IsDbNull(0) {
myInt = -1; // or whatever...
} else {
myInt = dataReader.GetInt32(0);
}
 
Put a break on it, then set a watch for dataReader[0]. It will tell you what
type it actually is. An int should be Int32, though, as you said.
 
Back
Top