Hi RPA,
Refer the below sample.
public partial class Form1 : Form
{
static int index = 0;
struct Question { public string question; }
struct Option { public string option1, option2, option3, option4; }
public Form1()
{
InitializeComponent();
GenerateQuestionsOptions(index);
}
private void btnNext_Click(object sender, EventArgs e)
{
if (index < PopulateQuestions().Rows.Count - 1)
{
index++;
GenerateQuestionsOptions(index);
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (index > 0)
{
index--;
GenerateQuestionsOptions(index);
}
}
public void GenerateQuestionsOptions(int index)
{
// Question based on Index
Question question = new Question();
question.question = PopulateQuestions().Rows[index]["QuestionDescription"].ToString();
// Options based on Question
Option options = new Option();
options.option1 = PopulateQuestions().Rows[index]["OptionOne"].ToString();
options.option2 = PopulateQuestions().Rows[index]["OptionTwo"].ToString();
options.option3 = PopulateQuestions().Rows[index]["OptionThree"].ToString();
options.option4 = PopulateQuestions().Rows[index]["OptionFour"].ToString();
// Adding options to List for shuffling options
List<string> optionsList = new List<string>();
optionsList.Add(options.option1);
optionsList.Add(options.option2);
optionsList.Add(options.option3);
optionsList.Add(options.option4);
// Shuffle options List
List<string> shuffledOptions = optionsList.OrderBy(a => Guid.NewGuid()).ToList();
// Assigning question and options
lblQuestion.Text = (index + 1) + " : " + question.question.ToUpper();
rbOption1.Text = shuffledOptions[0];
rbOption2.Text = shuffledOptions[1];
rbOption3.Text = shuffledOptions[2];
rbOption4.Text = shuffledOptions[3];
}
// Get question and options from database
private DataTable PopulateQuestions()
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM QuestionTable", con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
da.Fill(dt);
con.Close();
}
}
}
return dt;
}
}
Screenshot
