title: Checking the number of rows returned from MySQL Data Reader
body: <p>I am currently working on an C# project and I am trying to get the number of rows returned from MySQL Data Reader. </p>
  
  <p>I know there is no direct function so I am trying to write my own. In the function, I pass it the <code>MySQLDataReader</code> object and then loop through the MySQL Data Reader and increment a counter and return the value of the counter. </p>
  
  <p>This then seems to lock up the program, I guess because I am <code>Reader.read()</code> after I've got the count I'm already at the end. Instead I have tried creating a copy of the reader and then loop through the temp version but I get the same result. </p>
  
  <p>Below is my code where I am executing the query and calling the function. </p>
  
  <pre class="lang-cs prettyprint-override"><code>string query = "SELECT * FROM reports, software, platforms, versions "
                          + "WHERE EmailVerified = @verified AND reports.SoftwareID = software.id AND reports.PlatformID = platforms.id "
                          + "AND reports.VersionID = versions.id AND BugReportAcceptedNotificationSent = @notificationSent";
  using (MySqlCommand cmd = new MySqlCommand(query, db.conn))
  {
      cmd.Parameters.AddWithValue("@verified", "1");
      cmd.Parameters.AddWithValue("@notificationSent", "0");
      using (MySqlDataReader reader = cmd.ExecuteReader())
      {
          totalEmails = HelperClass.totalRowsInMySQLDataReader(reader);
          while (reader.Read())
          {
              currentEmailCount++;
              EmailNotifications emailNotification = new EmailNotifications(reader);
              emailNotification.sendNewBugReportAfterVerificationEmail(currentEmailCount, totalEmails);
          }
      }
  }
  </code></pre>
  
  <p>Below is my function that gets the row count</p>
  
  <pre class="lang-cs prettyprint-override"><code>public static int totalRowsInMySQLDataReader(MySqlDataReader reader)
  {
      MySqlDataReader tempReader = reader;
      ILibraryInterface library = GeneralTasks.returnBitsLibrary(Configuration.databaseSettings, Configuration.engineConfig.logFile);
      string methodInfo = classDetails + MethodInfo.GetCurrentMethod().Name;
      try
      {
          int count = 0;
          while (tempReader.Read())
          {
              count++;
          }
          tempReader = null;
          return count;
      }
      catch (Exception ex)
      {
          string error = string.Format("Failed to get total rows in MySQL Database. Exception: {0}", ex.Message);
          library.logging(methodInfo, error);
          library.setAlarm(error, CommonTasks.AlarmStatus.Medium, methodInfo);
          return -1;
      }
  }
  </code></pre>