C#C
C#3y ago
Mek

✅ System.Data.SQLiteDatabase .NET 7.0 Reading Items From Database

public static List<GameModel> GetAllGames()
{
    using SQLiteConnection? conn = new(dbFile);
    using SQLiteCommand? cmd = conn.CreateCommand();
    SQLiteDataReader? reader;

    List<GameModel> games = new();

    conn.Open();

    reader = cmd.ExecuteReader();

    while (reader.Read())
    {
        GameModel game = new()
        {
            Id = Convert.ToInt32(reader["Id"].ToString()),
            Username = reader["User"].ToString(),
            Date = reader["Date"].ToString(),
            StartTime = reader["StartTime"].ToString(),
            EndTime = reader["EndTime"].ToString(),
            Duration = reader["Duration"].ToString(),
            Score = Convert.ToInt32(reader["Score"].ToString()),
            Total = Convert.ToInt32(reader["Total"].ToString()),
            Difficulty = reader["Difficulty"].ToString(),
            GameType = reader["GameType"].ToString()
        };

        games.Add(game);
    }

    return games;
}
I use this exact code (change a couple of variable names) in my other version of the math game I created and it keeps hollering at me object reference not set to an instance of an object when it comes to reader = cmd.ExecuteReader();. What is happening? There are 2 records in the database, but it just will not read.
image.png
Was this page helpful?