Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6189679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:27:20+00:00 2026-05-24T02:27:20+00:00

I have code for a quiz but am unsure on how I count the

  • 0

I have code for a quiz but am unsure on how I count the total number of questions in my database. I know that I need a count query but i’m not sure where to insert it.
Here is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Data.Sql;
using System.Data.SqlClient;

namespace Quiz_Test
{
public partial class Form1 : Form
{
public Form1()
{
  InitializeComponent();
}

String chosenAnswer, correctAnswer;
DataTable table;
int questionNumber;

private void Form1_Load(object sender, EventArgs e)
{
  string cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";

  OleDbConnection conGet = new OleDbConnection(cnString);
  OleDbCommand cmdGet = new OleDbCommand();

  conGet.Open();

  cmdGet.CommandType = CommandType.Text;
  cmdGet.Connection = conGet;

  cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()";

  OleDbDataReader reader = cmdGet.ExecuteReader();
  table = new DataTable();
  table.Load(reader);

  foreach (DataRow row in table.Rows)
  {
    labelQuestion.Text = table.Rows[0]["Question"].ToString();   
    radioButton1.Text = table.Rows[0]["Answer 1"].ToString(); 
    radioButton2.Text = table.Rows[0]["Answer 2"].ToString();
    radioButton3.Text = table.Rows[0]["Answer 3"].ToString();
    radioButton4.Text = table.Rows[0]["Answer 4"].ToString();

    correctAnswer = table.Rows[0]["Correct Answer"].ToString(); ;
    questionNumber = 0;
  }
  conGet.Close();

}

private void btnGoToNextOne_Click(object sender, EventArgs e)
{
  String cnString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Hannah\\Desktop\\QuizQuestions.accdb";

  OleDbConnection conGet = new OleDbConnection(cnString);
  OleDbCommand cmdGet = new OleDbCommand();

  {
    conGet.Open();

    cmdGet.CommandType = CommandType.Text;
    cmdGet.Connection = conGet;

    cmdGet.CommandText = "SELECT * FROM QuizQuestions ORDER BY rnd()";

    OleDbDataReader reader = cmdGet.ExecuteReader();
    reader.Read();

    if (radioButton1.Checked)
    {
      chosenAnswer = reader["Answer 1"].ToString();
    }
    else if (radioButton2.Checked)
    {
      chosenAnswer = reader["Answer 2"].ToString();
    }
    else if (radioButton3.Checked)
    {
      chosenAnswer = reader["Answer 3"].ToString();
    }
    else if (radioButton4.Checked)
    {
      chosenAnswer = reader["Answer 4"].ToString();
    }

    if (chosenAnswer == reader["Correct Answer"].ToString())
    {

        labelQuestion.Text = table.Rows[questionNumber]["Question"].ToString();
        //and show possible answers:
        radioButton1.Text = table.Rows[questionNumber]["Answer 1"].ToString();
        radioButton2.Text = table.Rows[questionNumber]["Answer 2"].ToString();
        radioButton3.Text = table.Rows[questionNumber]["Answer 3"].ToString();
        radioButton4.Text = table.Rows[questionNumber]["Answer 4"].ToString();
        correctAnswer = table.Rows[questionNumber]["Correct Answer"].ToString();
        questionNumber++;

    }
    else
    {
      MessageBox.Show("That is not the correct answer");
    }
  }
}

}
}

I know I need to put in “SELECT count(*) from QuizQuestions” but I’m not sure how I can determine the ‘position’ in the set of questions so that I don’t get this error:

IndexOutOfRangeException was unhandled
There is no row at position 5
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T02:27:22+00:00Added an answer on May 24, 2026 at 2:27 am

    It’s the “table.Rows[questionNumber]” that is killing you, you need to do a

    table.Rows.Count check in that area. Something like

    if (questionNumber < table.Rows.Count )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a set of quiz game questions in a sql database (javascript and
I have a bit of code I wrote, that echo's 3 variable (quiz name,
I have a MySQL Database for a basic quiz program I'm making. The questions
I have code that references a web service, and I'd like the address of
I have code that looks like the following, which works fine for displaying the
I have a questions.xml file which has a list of questions for my quiz,
i am trying to generate random number for my mental math quiz game. But
I have code that opens a dialog, validates, and posts a form when the
I have this code here: case 'resource_list': if(file_exists('content.php')){ include('../ajax/content.php'); } else { die('does not
I have following javascript code. Its a timer code. Timer stops if quiz is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.