views:

48

answers:

2

I am facing a strange issue with DataReader. I am using OdbcClient with a legacy rdbms system.

I am sending follwing command to the database.

select Col1, Col2, Col3 from Table1 where Col2 = 'Val1';

However in certain cases when I iterate it through DataReader as shown below.

IDataReader reader = cmd.ExecuteReader();

while(reader.Read())
{
  // Get the values from reader

  int col2 = reader.GetValue(1);

}

I get the Col2 value as DBNull inspite of the fact that I have value in the Col2 column in the database. I am getting the values of Col1 and Col3 correctly. What can be the possible reasons of this behaviour?

Update : This is fixed if I call cmd.Prepare() before calling cmd.ExecuteQuery(). Please can anyone explain this behaviour?

A: 

What is the data type of Col2? you are reading with GetValue, and maybe the database has a value that is not convertible to Int... is it the case?

Jhonny D. Cano -Leftware-
A: 

Have you tried to read with named argument:

int col2 = int.parse(reader["Col2"].ToString());

?

JohnKZ