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.
string quizid = "";
// Open DB connection.
conn.Open();
string cmdText = "SELECT MIN (QuizID) FROM dbo.QUIZ WHERE IsSent <> 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();
}
When I used MIN, it gave me the following error:
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.Exception Details: System.IndexOutOfRangeException: QuizID
Source Error:
> Line 69: {
> Line 70: //
> There is only 1 column, so just retrieve it using the ordinal position
> Line 71: quizid = reader["QuizID"].ToString();
> Line 72:
> Line 73: }
Or, (along with the correct answers)
you can name that column as you would like