Topic: SQLite IDataReader

Hello All!
I am going to crazy and feeling myself so stupid but I don't understand such behaviour.
I have code:
public int getNextAgentId()
{
Int32 agent_id = 0;
IDataReader dr = dbap.DBDataReader("SELECT MAX(agent_id) FROM Agents");
if(dr.Read())
{
agent_id = dr.GetInt32(0);
}
dr.Close();
return agent_id + 1;
}

where dpab is an instance of DB abstraction class, hence using IDataReader interface instead of
concrete DB DataReader such as SqlDataReader.

SELECT returns null due to database table is empty and I have
error message "Data is Null. This method or property cannot be called on Null values."
on line agent_id = dr.GetInt32(0);
As I understand in the begining datareader positioned before first reacord and when I call Read method
it should point to first record but why it returns true in conditions when now rows returned from DB?

Any ideas?
Thanks in advance to all.

Regards,
neeraj singh panwar
soccer uniform | cheap jerseys | football shirt shop

Last edited by Neeraj555 (2013-09-13 08:44:11)

Re: SQLite IDataReader

it's good advice. The only thing I would add is consider the command's ExecuteScalar method when retrieving a single value from the database. And if you do use the datareader, use the CommandBehavior.CloseConnection option and always immediately read the data and then close the datareader.

A comment about the TableAdapter. The .Net TableAdapter ultimately uses the Data Provider namespace's DataAdapter. Using the Fill method of the DataAdapter (for example, SqlDataAdapter) is equivalent to using the TableAdapter's Fill method.


Spider Injector

Last edited by johansmith (2011-05-24 05:47:21)