Hi,
Instead of using Array you can use DataTable to save the user selected id, question and the provided answer and save in database.
Refer below example created using the below link.
HTML
<div>
<div id="dvQuestion" runat="server">
<h1>
<asp:Label ID="lblQuestion" runat="server" />
</h1>
<br />
<asp:RadioButtonList ID="rbtnOptions" runat="server">
</asp:RadioButtonList>
<br />
<br />
<asp:Button ID="Button1" Text="Next" runat="server" OnClick="Next" />
</div>
<div id="dvResult" runat="server" visible="false">
<h1>
<asp:Label ID="lblResult" runat="server" />
</h1>
<br />
<asp:Button ID="Button2" Text="Start Again" runat="server" OnClick="Start" />
</div>
<br />
<asp:GridView ID="gvAnswered" runat="server" />
</div>
Namespaces
C#
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web.UI.WebControls;
Code
C#
private DataTable DataTableQuestions
{
get { return (DataTable)ViewState["Question"]; }
set { ViewState["Question"] = value; }
}
private DataTable Answered
{
get { return (DataTable)ViewState["Answer"]; }
set { ViewState["Answer"] = value; }
}
private int QuestionIndex
{
get { return (int)ViewState["QuestionIndex"]; }
set { ViewState["QuestionIndex"] = value; }
}
private int Score
{
get { return (int)ViewState["Score"]; }
set { ViewState["Score"] = value; }
}
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTableQuestions = PopulateQuestions();
QuestionIndex = 0;
Score = 0;
GetCurrentQuestion(QuestionIndex, DataTableQuestions);
}
}
protected void Next(object sender, EventArgs e)
{
QuestionIndex++;
GetCurrentQuestion(QuestionIndex, DataTableQuestions, int.Parse(rbtnOptions.SelectedItem.Value));
}
protected void Start(object sender, EventArgs e)
{
Response.Redirect(Request.Url.AbsoluteUri);
}
private DataTable PopulateQuestions()
{
DataTable dt = new DataTable();
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection _cn = new SqlConnection(constr))
{
using (SqlCommand _cmd = new SqlCommand("SELECT * FROM QuestionTable", _cn))
{
using (SqlDataAdapter da = new SqlDataAdapter(_cmd))
{
_cn.Open();
da.Fill(dt);
_cn.Close();
}
}
}
return dt;
}
private void GetCurrentQuestion(int index, DataTable dtQuestions, int? selectedOption = null)
{
DataTable dtAnswered = new DataTable();
if (selectedOption != null)
{
string option = string.Empty;
DataRow row = dtQuestions.Rows[index - 1];
if (selectedOption == 0)
{
option = "OptionOne";
}
else if (selectedOption == 1)
{
option = "OptionTwo";
}
else if (selectedOption == 2)
{
option = "OptionThree";
}
else if (selectedOption == 3)
{
option = "OptionFour";
}
if (option == row["CorrectAnswer"].ToString())
{
Score++;
}
if (ViewState["Answer"] == null)
{
dtAnswered.Columns.AddRange(new DataColumn[3] { new DataColumn("QId", typeof(int)),
new DataColumn("Question", typeof(string)),
new DataColumn("AnswerGiven",typeof(string)) });
dtAnswered.Rows.Add(row["QuestionId"], row["QuestionDescription"], option);
ViewState["Answer"] = dtAnswered;
}
else
{
dtAnswered = (DataTable)ViewState["Answer"];
dtAnswered.Rows.Add(row["QuestionId"], row["QuestionDescription"], option);
ViewState["Answer"] = dtAnswered;
}
}
if (index < dtQuestions.Rows.Count)
{
DataRow row = dtQuestions.Rows[index];
lblQuestion.Text = row["QuestionDescription"].ToString();
List<ListItem> options = new List<ListItem>();
ListItem option1 = new ListItem(row["OptionOne"].ToString(), "0");
ListItem option2 = new ListItem(row["OptionTwo"].ToString(), "1");
ListItem option3 = new ListItem(row["OptionThree"].ToString(), "2");
ListItem option4 = new ListItem(row["OptionFour"].ToString(), "3");
options.AddRange(new ListItem[4] { option1, option2, option3, option4 });
List<ListItem> randomOptions = RandomizeList(options);
rbtnOptions.Items.Clear();
rbtnOptions.Items.AddRange(randomOptions.ToArray());
rbtnOptions.DataBind();
}
else
{
dvQuestion.Visible = false;
dvResult.Visible = true;
lblResult.Text = string.Format("You Scored {0}/{1}", Score, index);
// Write code to save the answered given by user in Database.
gvAnswered.DataSource = dtAnswered;
gvAnswered.DataBind();
}
}
public List<ListItem> RandomizeList(List<ListItem> originalList)
{
List<ListItem> randomList = new List<ListItem>();
Random random = new Random();
ListItem value = default(ListItem);
while (originalList.Count() > 0)
{
var nextIndex = random.Next(0, originalList.Count());
value = originalList[nextIndex];
randomList.Add(value);
originalList.RemoveAt(nextIndex);
}
return randomList;
}
Screenshot
