title: How to get all my database table values in a listbox with the for statement C#
body: <p>As a beginner in C# I am having some problems with getting all my values (and only the values - not the field names) out of my database table and list them in my listbox. </p>
  
  <p>I did some research and found out that I could get all the field names of a specific table with the following code:</p>
  
  <pre><code>try
   {
     connection.Open();
     OleDbCommand command = new OleDbCommand();
      command.Connection = connection;
     string query = "select * from MyTable where Month='January'";
  
     command.CommandText = query;
     OleDbDataReader reader = command.ExecuteReader();
     var columns = listBox5;
  
     for (int i = 0; i &lt; reader.FieldCount; i++)
     {
        if (reader.GetName(i) != "Month" &amp;&amp; reader.GetName(i) != "Id")
        {
                          columns.Items.Add(reader.GetName(i));
        }    
     }
  
  
     connection.Close();
  }
  catch (Exception ex)
  {
       MessageBox.Show("Error" + ex);
  }
  </code></pre>
  
  <p>This works perfectly for listing the field names (and not the field names Month and ID). Though now I also need to list all my values (that match with the corresponding field names). At first I thought to add them at the same time with the field names (above code) but this was to complicated for me. So then I try'd to list the values in another listbox with kinda the same code, but instead of using the GetName I used the GetValue. See code below:</p>
  
  <pre><code>try
  {
     connection.Open();
     OleDbCommand command = new OleDbCommand();
     command.Connection = connection;
     string query = "select * from MyTable";
  
     command.CommandText = query;
     OleDbDataReader reader = command.ExecuteReader();
  
     var columns2 = listBox6;
     for (int i = 0; i &lt; reader.FieldCount; i++)
     {    
        columns2.Items.Add(reader.GetValue(i).ToString());    
     }
     connection.Close();
  }
  catch (Exception ex)
  {
    MessageBox.Show("Error" + ex);
  }
  </code></pre>
  
  <p>This doesn't work though. I am getting the following error:</p>
  
  <blockquote>
    <p>ErrorSystem.InvalidOperationException: Data doesn't exist for the row/column at line ...</p>
  </blockquote>
  
  <p>On this line:</p>
  
  <pre><code>columns2.Items.Add(reader.GetValue(i).ToString());
  </code></pre>