Why I got an error in the system when I use MIN in the SQL Query?
<p>I am developing a web application that sends the quizzes to all users via emails. If there is more than one quiz in the database that not be sent to the users before, the system should select the minimum quiz id not the maximum one. </p>

<pre><code> string quizid = "";

 // Open DB connection.
 conn.Open();

 string cmdText = "SELECT MIN (QuizID) FROM dbo.QUIZ WHERE IsSent &lt;&gt; 1";
 using (SqlCommand cmd = new SqlCommand(cmdText, conn))
 {
     SqlDataReader reader = cmd.ExecuteReader();
     if (reader != null)
     {
         while (reader.Read())
        {
             // There is only 1 column, 
             // so just retrieve it using the ordinal position
             quizid = reader["QuizID"].ToString();
        }
      }
      reader.Close();
   }
</code></pre>

<p>When I used MIN, it gave me the following error:</p>

<blockquote>
  <p>Description: An unhandled exception occurred during the execution of
  the current web request. Please review the stack trace for more
  information about the error and where it originated in the code. </p>
  
  <p>Exception Details: System.IndexOutOfRangeException: QuizID</p>
  
  <p>Source Error: </p>
</blockquote>

<pre><code>&gt; Line 69:                     { 
&gt; Line 70:                         //
&gt; There is only 1 column, so just retrieve it using the ordinal position
&gt; Line 71:                         quizid = reader["QuizID"].ToString();
&gt; Line 72:  
&gt; Line 73:                     }
</code></pre>
