Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8955991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:41:02+00:00 2026-06-15T14:41:02+00:00

I am retrieving data from a database and trying to populate a List<>. That

  • 0

I am retrieving data from a database and trying to populate a List<>. That is my goal.

I am currently inserting the retrieved data into variables within a method and then trying to add the method to the List<>.

Here is my code:

public void Select()
    {
        var sprite = new Sprite();

        if (Game.player.inBattle)
        {
            //Open a connection
            databaseCon.Open(); //Works

            //Create Command
            var cmd = new MySqlCommand(BattleEngine.query, databaseCon); //Works

            //Create a data reader and Execute the command
            var dataReader = cmd.ExecuteReader(); //Works

            try
            {

                //Read the data and store them in the list
                while (dataReader.Read())
                {
                    //Inserts matching Row's first Column attribute into method variable

                    sprite.id = (dataReader.GetInt32(0));
                    //Inserts matching Row's second Column attribute into method variable

                    sprite.identifier = (dataReader.GetString(1)); 

                    Console.WriteLine(sprite.id + ": " + sprite.identifier); //Works. Variables are loaded with data

                    //Fails. Doesn't load the method into the list
                    Game.sprite.Add(new Sprite()); 
                }
            }
            catch (MySqlException ex)
            {
                Console.WriteLine("Error: {0}", ex);

            }
            finally
            {
                if (dataReader != null)
                {
                    //close Data Reader
                    dataReader.Close();
                }

                if (databaseCon != null)
                {
                    //close Connection
                    databaseCon.Close();
                }
            }
        }
    }

I then have a test Console.WriteLine(Game.sprite[0]); setup after this loops and it displays Namespace.Sprite

There are probably other ways of doing this but ultimately I would like to have the list populated with each record within a new method. By storing it in the List<> I can then pull specific information to be used later.

In theory I want to be able to do this;

Console.WriteLine("{0}", Game.sprite[0].id);

I have seen this done before but I cannot seem to replicate it.

ADDITION:

public class Game
{
    public static Player player;
    public static List<Character> sprite;
    GameScreens mainMenu = new GameScreens();
    GameScreens titleScreen = new GameScreens();

    public Game()
    {
        Console.SetWindowSize(100, 50);
        Console.BufferHeight = 50;
        Console.BufferWidth = 100;

        titleScreen.TitleScreen();

        player = new Player();
        sprite = new List<Character>();
        Player.Initialize(player);

        GameLoop();
    }

    void GameLoop() //Our game loop.
    {
        mainMenu.MainMenu();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:41:04+00:00Added an answer on June 15, 2026 at 2:41 pm

    Try:

    Game.sprite.Add(sprite);

    Instead of:

    Game.sprite.Add(new Sprite());

    Your code adds an empty Sprite object – not the one you declared

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm facing that problem when trying to retrieving data from database: Error parsing data
Currently, I'm retrieving some data from a MySQL database table, but the problem is
I'm trying to declare an enum type based on data that I'm retrieving from
i am using cursor for retrieving data from database but problem is that i
I did the pagination for data I'm retrieving from the database. when I'm trying
I am trying to fetch data from SQLite database(NSArray of strings) and populate the
I am trying to write a query for retrieving data from SQLite database. I
I am retrieving a fetch from my core data database and trying to iterate
For some reason I'm having a problem retrieving data from my database. It leaves
I am retrieving data from an Oracle database and binding the same to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.