Hi RPA,
Refer the below sample.
SQL
CREATE PROCEDURE GetCorrectAnswer
@Question VARCHAR(MAX)
AS
BEGIN
DECLARE @Correct VARCHAR(50)
SELECT @Correct = CorrectAnswer FROM QuestionTable WHERE QuestionDescription = @Question
SELECT CASE @Correct
WHEN 'OptionOne' THEN (SELECT OptionOne FROM QuestionTable WHERE QuestionDescription = @Question)
WHEN 'OptionTwo' THEN (SELECT OptionTwo FROM QuestionTable WHERE QuestionDescription = @Question)
WHEN 'OptionThree' THEN (SELECT OptionThree FROM QuestionTable WHERE QuestionDescription = @Question)
WHEN 'OptionFour' THEN (SELECT OptionFour FROM QuestionTable WHERE QuestionDescription = @Question)
END Answer
FROM QuestionTable
WHERE QuestionDescription = @Question
END
Code
static int index = 0;
static string answer = "";
static int correct = 0;
public struct Question { public string question; }
public struct Option { public string option1, option2, option3, option4; }
public Form1()
{
InitializeComponent();
GenerateQuestionsOptions(index);
lblAnswer.Visible = false;
}
private void btnNext_Click(object sender, EventArgs e)
{
foreach (Control control in groupBox1.Controls)
{
if (control.GetType().Name.ToLower() == "radiobutton")
{
if ((control as RadioButton).Checked)
{
if (answer.ToLower() == (control as RadioButton).Text.ToLower())
{
correct++;
}
}
}
}
if (index < PopulateQuestions().Rows.Count - 1)
{
index++;
GenerateQuestionsOptions(index);
lblAnswer.Visible = false;
}
else
{
groupBox1.Visible = false;
btnNext.Visible = false;
lblAnswer.Visible = true;
lblAnswer.Text = correct + " correct answer out of " + PopulateQuestions().Rows.Count + " question";
}
}
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();
// Answer based on Question
answer = PopulateCorrectAnswer(PopulateQuestions().Rows[index]["QuestionDescription"].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();
//List<string> shuffledOptions = RandomizeList(optionsList);
// 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];
}
public List<string> RandomizeList(List<string> originalList)
{
List<string> randomList = new List<string>();
Random random = new Random();
string value = default(string);
while (originalList.Count() > 0)
{
var nextIndex = random.Next(0, originalList.Count());
value = originalList[nextIndex];
randomList.Add(value);
originalList.RemoveAt(nextIndex);
}
return randomList;
}
// 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;
}
//Get answer for question from database
private string PopulateCorrectAnswer(string questionDescription)
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("GetCorrectAnswer", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Question", questionDescription);
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
da.Fill(dt);
con.Close();
}
}
}
return dt.Rows[0].ItemArray[0].ToString();
}
Screenshot
Page Design with DataBase Record

Output
