Click here to Skip to main content
15,886,545 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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">////////////////////////DISPLAY METHOD///////////////////////////////
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;
}
    ////////////////////////NEXT METHOD///////////////////////////////
    protected void Button2_Click(object sender, EventArgs e)
{
    try
    {
        Button1.Enabled = true;
        Label4.Visible = false;
        n = n + 1;
       // selected = (string)Session["selected"];
        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;
    }
    }
    ////////////////////////prev METHOD///////////////////////////////
    protected void Button1_Click(object sender, EventArgs e)
    {try
        {
            Button2.Enabled = true;
            Label4.Visible = false;
           // selected = (string)Session["selected"];
                    n = n - 1;
                    if (n == 0)
                    {
                        //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;
    }
    }

Posted
Updated 26-Jul-11 9:12am
v2
Comments
R. Giskard Reventlov 26-Jul-11 11:17am    
Homework: ask your tutor or pay more attention in class or RTFM.
asmaa elbattanony 26-Jul-11 11:24am    
Thank u for your reply
homework or not
I actually made an effort in this application
and i didn't ask to give me the full code
I'm having a problem in my code and I'm looking for help
Isn't this what this site is all about???
thank you again :)
Christian Graus 26-Jul-11 15:13pm    
You're clearly stupid. You posted this great swathe of code over and over, instead of editing your post. I fixed it for you. I refuse to read that much code tho, if you can't understand your code enough to ask based on a smaller snippet, you're on your own.
asmaa elbattanony 27-Jul-11 8:42am    
Just because its my first time to make a post in here and i made some mistakes now this means that I'm stupid???
OK well thank you for your (GREAT HELP)
I'm waiting for someone who can actually help me though

Hi,
Maybe these projects may help you and save some of your time too:
This is a survey project build with asp.net forms and .net 4.0 http://survey.codeplex.com/[^]

or if you are interested in asp.net mvc framework project this one could help you too http://surveymaster.codeplex.com/[^]

B.R
 
Share this answer
 
You must store your answer in a Session variable as soon as the user moves to the next question, and when the user goes back to the previous question, all you have to do is to check the Session to see if the question is already answered: if yes, then restore the answer from the session and if no, then restore it from the database.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900