0

I have a listBox1 in my MainForm that I want to fill with the results from a MySQL query (results are put into 'tmpPeers'). The listBox1 keeps giving me the error above. here's the code:

internal async Task<Account> FromSchool(string school)
        {
            var tmpCommand = Core.Instance.Database.Connection.CreateCommand();

            tmpCommand.CommandText =
                $@"SELECT `name`, `grade`, `hobbies`, FROM `accounts` WHERE `school` = '{school}';";
            var tmpPeers = await tmpCommand.ExecuteReaderAsync();

            if (!tmpPeers.HasRows)
                return null;

        tmpPeers.Close();


        MainForm.listBox1.Items.Add(tmpPeers);


        return null;
        }

please help! I have pretty limited coding knowledge.

I want the results from the query to fill the listBox, but right now I run it and it is empty.

EDIT: THIS IS NOT THE SAME AS THE MARKED DUPLICATE. The method in my problem is already static, yet the error still comes up. Resharper prompts me to make the field listBox1 static, but when I do i get the error "Member 'MainForm.listBox1' cannot be accessed with an instance reference; qualify it with a type name instead.

Kai
  • 27
  • 5
  • 1
    Have you even searched a bit before? This question has been posted thousand of times before with exactly the same message. – MakePeaceGreatAgain Jan 10 '18 at 13:44
  • Try removing MainForm and write `listBox1.Items.Add(tmpPeers);` – M.kazem Akhgary Jan 10 '18 at 13:45
  • If this is not a duplicate of the linked question, please provide a [mcve] reproducing the problem. With that we're much more likely to be able to help. See also [ask] and https://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/ – dbc Jan 10 '18 at 19:54
  • It's the same problem as the linked question. When you reference a member like with a class like `MainForm.listBox1;` it needs to be static; when you reference it like `var mainForm = new MainForm(); mainForm.listBox1;` it cannot be marked static. The correct approach here is to *not* use `MainForm.listBox1;` but to find and use the instance of `MainForm`. – Kirk Broadhurst Jan 10 '18 at 19:59

0 Answers0