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?