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

时间:2016-10-15 17:03:01

标签: c#

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; 
}

1 个答案:

答案 0 :(得分:1)

您的数据已被检索并存储在列表中。 在while循环中,Reader正在枚举结果集,并已移至没有数据的索引。

请使用列表(列表和名称)进一步处理该数据。

旁注 - 您检索的数据来自表格并且是相关的,因此您可能希望使用字典而不是列表或其他复杂对象。