Reader losing values after sqlite query

Viewed 24

So I'm attempting to read in values from an existing SQLite database and it seems to initially work but after the query it seems to lose the values when I try to read them in.

What am I doing wrong here?

https://www.youtube.com/watch?v=Yvj6BTjts3M

public ObservableCollection<Asset> GetAllAssets()
{
    _DbConn.Open();

    var command = _DbConn.CreateCommand();
    var allAssets = new ObservableCollection<Asset>();

    command.CommandText =
    @"
        SELECT * 
        FROM asset 
    ";

    using (var reader = command.ExecuteReader())
    {
        while (reader.HasRows)
        {
            Asset asset = new Asset();
            asset.AssetId = reader.GetInt32(0);
            asset.AssetTypeId = reader.GetInt32(1);
            asset.Name = reader.GetString(2);
            asset.FileLocation = reader.GetString(3);
            asset.IsCustom = reader.GetBoolean(4);
            asset.AssetSectionId = reader.GetInt32(5);
            asset.ThumbFileLocation = reader.GetString(6);

            allAssets.Add(asset);
        }
    }

    _DbConn.Close();

    return allAssets;
}
1 Answers

The method HasRows() returns true if reader contains one or more rows or false if not, but it does not advance reader to the next row.

You must use the method Read():

while (reader.Read())
......................
Related