© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
61 replies
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;
}
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
object reference not set to an instance of an object
when it comes to
reader = cmd.ExecuteReader();
reader = cmd.ExecuteReader();
. What is happening? There are 2 records in the database, but it just will not read.
image.png
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements
Next page

Similar Threads

❔ ✅ Issue with data reading from database
C#CC# / help
3y ago
❔ .net 7, net 7.0.3 compatibility issues? in docker
C#CC# / help
3y ago
✅ .NET 8 migration from .net 7
C#CC# / help
2y ago
✅ display data from database
C#CC# / help
3y ago