I have not used DataReaders in ages (I prefer to use ORM), but I have to work. I unlock the lines and verify that HasRows true; debugging at this stage and examining the reader shows that my data is there.
Now here is the problem: the moment I call reader.Read() , trying to expand the results, it says: “Enumeration did not produce any results” or something else, and I get “Invalid attempt to read when there is no data”. error. I get the same if I do not call Read() (this is the default value since the DataReader runs before the first write).
I cannot remember the right way to handle this; the data is there when I check HasRows , but left at the moment when I either try to read it right after or after I call Read , which does not make sense, as if I did not call Read , the reader should still be up to of the first record, and if a property is set that starts it in the first record (SingleRow? Have I forgotten its name), then I should be able to read lines without calling Read, however both paths seem to be moved beyond the line containing the data.
What am I forgetting? The code is pretty simple:
TemplateFile file = null; using (DbDataReader reader = ExecuteDataReaderProc("GetTemplateByID", idParam)) { if (reader.HasRows) // reader has data at this point - verified with debugger { reader.Read(); // loses data at this point if I call Read() template = new TemplateFile { FileName = Convert.ToString(reader["FileName"]) // whether or not I call // Read, says no data here }; } }
source share