Nabeel Aslam
Nabeel Aslam

Reputation: 11

Invalid attempt to read when no data is present at Reader show empty

system.invalid operation exception invalid attempt to read when no data is present after while loop Reader show Empty.

byte[] fingerprint()   
{
    OpenConnection();
    SqlCommand command = connection.CreateCommand();
    SqlDataReader Reader;
    command.CommandText = "SELECT fp,name FROM fptable";
    Reader = command.ExecuteReader();
    // Reader= command.ExecuteReader(CommandBehavior.CloseConnection);
    try
    {
        while (Reader.Read())
        {
            Reader.GetValue(0);
            String name = Reader.GetValue(1) as String;
            byte[] temps = Reader.GetValue(0) as byte[];
            list.Add(temps);
            names.Add(name);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
    byte[] temp = Reader.GetValue(0) as byte[]; //**error at this point**
    // byte[] temp = Reader["fp"] as byte[];
    // MakeReport(list.Count+""+names.Count);
    return temp; 
}

Upvotes: 0

Views: 110

Answers (1)

Dharminder Singh
Dharminder Singh

Reputation: 64

Your data has been already retrieved and stored in the list. In the while loop the Reader is enumerating on the resultset and has moved to the index where no data is present.

Please use the List (list and names) to work further on that data.

On side note - The data you have retrieved is from a table and related, so you might want to use dictionary instead of the list or some other complex object.

Upvotes: 1

Related Questions