Hi everyone,
I'm developing an online examination site using asp.net & c#.
I'm having a silly problem, I can't store and retrieve the student's answer
meaning that if the student chose b for example as the answer to the first question
then clicked next button then clicked previous button his answer will not be selected .
and that is my problem
I'm using a radio button list to display the choices and I'm storing the correct answer as well as the selected answer in the database(sql server 2005)
then i read the data from the table and stored it in a datatable
after that i made a method to display the data from the datatable(including the selected answer) and made the selected item equal the selected answer in the datatable
but this created the problem which is the selected index of the radio button list is always set to the selected answer from the database not the one i changed
although i made an update operation to this field(selected answer) in runtime still can't change the selected index to the new one and only sees the index from the db
Any help?
Shameel
Thank u for your help
i made something like your idea but still nothing here is my code(display method,next button and previous button actions)
<pre lang="cs">public void display()
{
DataRow dr=t.Rows[n];
Label3.Text=dr["q"].ToString();
RadioButtonList1.Items[0].Text = dr["a1"].ToString();
RadioButtonList1.Items[1].Text = dr["a2"].ToString();
RadioButtonList1.Items[2].Text = dr["a3"].ToString();
RadioButtonList1.Items[3].Text = dr["a4"].ToString();
Label4.Text = dr["c_ans"].ToString();
int index;
if (Session["selected"] != null)
{
index = RadioButtonList1.Items.IndexOf(RadioButtonList1.Items.FindByText(selected));
}
else
{
index = RadioButtonList1.Items.IndexOf(RadioButtonList1.Items.FindByText(dr["s_ans"].ToString()));
}
Response.Write(index);
RadioButtonList1.Items[index].Selected = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
try
{
Button1.Enabled = true;
Label4.Visible = false;
n = n + 1;
string sql = "update questions set s_ans='" + selected + "' where q='" + Label3.Text + "'";
Response.Write(sql);
comm = new SqlCommand(sql, conn);
int x = comm.ExecuteNonQuery();
if (n >= t.Rows.Count - 1)
{
Button2.Enabled = false;
}
Session["selected"] = selected;
RadioButtonList1.SelectedIndex = -1;
Session["n"] = n;
display();
}
catch (Exception ex)
{
Label4.Visible = true;
Label4.Text = "Please Choose an answer "+ex.Message;
}
}
protected void Button1_Click(object sender, EventArgs e)
{try
{
Button2.Enabled = true;
Label4.Visible = false;
n = n - 1;
if (n == 0)
{
Button1.Enabled = false;
}
string sql = "update questions set s_ans='" + selected + "' where q='" + Label3.Text + "'";
Response.Write(sql);
comm = new SqlCommand(sql, conn);
int x = comm.ExecuteNonQuery();
Session["selected"] = selected;
RadioButtonList1.SelectedIndex = -1;
Session["n"] = n;
display();
}
catch (Exception ex)
{
Label4.Visible = true;
Label4.Text = "Please Choose an answer "+ex.Message;
}
}