Search code examples
c#sqlitedatareader

Retrieved data not read in while loop using SQLiteDataReader.Read() method in C#


SQLiteDataReader.Read() doesn't reply with false even though it contains data.

  1. I have been running tests on some data controllers, but every time I retrieve data from the db it does not work for some reason, to be exact, Read() doesn't enter the loop to read the data in it.
  2. Connection was successful, data has been read, and I have observed it in the debugger, please see the screenshot below.

The following code is what I'm doing, expected to enter the while loop in the try block.


    using (SQLiteConnection connection = new SQLiteConnection(connectionString))
    {
        SQLiteCommand command = new SQLiteCommand(connection);
        SQLiteDataReader reader = null;
        //Make sure to ignore class names below, the problem isn't related to them they are changed for sake of demonstration
        Dictionary<string, SomeDTO> output = new Dictionary<string, SomeDTO>();

        try
        {
            command.CommandText = $"SELECT * FROM {TableName}";
            
            connection.Open();
            reader = command.ExecuteReader();

            while (reader.Read())
            { //This loop is never entered
                output.Add(reader.GetString(0), ConvertReaderToObject(reader));
            }
        }
        catch (Exception ex)
        {
            Log.Error("SOME-LOG IRRELEVANT"+ex.Message);
        }
        finally
        {
            if(reader != null)
            {
                reader.Close();
            }
            command.Dispose();
            connection.Close();
        }

        return output;
    }
}

In the picture below you can see debugger details on the reader object, data was read as expected... proof data was read

I have reviewed these questions but couldn't find the answer there:

1, 2, 3


Solution

  • @TimSchmelter answer, posting to resolve so question won't drive attention to it. The debugger itself consumes the data in reader, thus I can see the data in the debugger. Debugging straight from the catch, will show I had an exception inside the while loop.