title: Get invalid attempt to access a field before calling Read() , but Read() is called in C#
body: <p>alright, I am trying to get data from MySQL with C# using MySQL connector 6.9.5, and I get this error messages :</p>
  
  <pre><code>Error: MySql.Data.MySqlClient.MySqlException: Invalid attempt to access a field before callingRead()
  at MySql.Data.MySqlClient.ResultSet.get_Item(Int32 index)
  at MySql.Data.MySqlClient.MySqlDataReader.GetFieldValue(Int32 index, Boolean checkNull)
  at MySql.Data.MySqlClient.MySqlDataReader.GetString(Int32 i)
  at PayrollManagementSystem.ConnectorDB.login(String username, String password) in C:\Users\Scarlet\Documents\Visual Studio 2008\Projects\PayrollManagementSystem\PayrollManagementSystem\ConnectorDB.cs:line 41
  </code></pre>
  
  <p>it means that I am trying to access a field before calling Read() method, but I think I already did, here's my code :</p>
  
  <pre><code>public ModelUser login(String username, String password)
      {
          Console.WriteLine("username asal = " + username);
          ModelUser val = null ;
          connection.ConnectionString = connectionString;
          try
          {
  
              command = new MySqlCommand( "SELECT user.id_data_user, user.username, user.password FROM user WHERE username = '" + username + "'" +
                  " AND password = '" + password + "'", connection);              
  
              MySqlDataReader reader;
  
              connection.Open();
  
              reader = command.ExecuteReader();
              val = new ModelUser();
  
              reader.Read(); == this is the Read() method was called
  
              val.IDDataUser = reader.GetString(0); === error at this line
              val.Username = reader.GetString(1);
              val.Password = reader.GetString(2);
  
  
              Console.WriteLine("data_user di conDb: " + val.IDDataUser);
  
              connection.Close();
  
          }
          catch (MySqlException ex)
          {
              Console.WriteLine("Error: {0}", ex.ToString());
          }
          return val;
  
      }
  </code></pre>
  
  <p>I dont know what's wrong with my code, well this is my first time try C#, I appreciate for any reply to my question</p>
  
  <blockquote>
    <p><strong>Update:</strong></p>
  </blockquote>
  
  <p>i changed my code like this</p>
  
  <pre><code>   if (reader.HasRows)
              {
                  while (reader.Read())
                  {
                      val.IDDataUser = reader.GetString(0);
                      val.Username = reader.GetString(1);
                      val.Password = reader.GetString(2);
                  }
  
              }
              else
              {
                  Console.WriteLine("Holy cow,she's got no rows!");
              }
  </code></pre>
  
  <p>my reader has no row. it's like it can't get any data from database. and this is what the logs look like :</p>
  
  <pre><code>'PayrollManagementSystem.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dl 
  ', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  'PayrollManagementSystem.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_64\System.EnterpriseServices\2.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
  Holy cow,she's got no rows!
  username: 
  data user: 
  id : 
  The thread '&lt;No Name&gt;' (0x13f0) has exited with code 0 (0x0).
  </code></pre>