Click here to Skip to main content
15,850,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following code is for when user clicks next question button..(working good)
my doubt is how can i change this code for clicking previous question button.. please help(i=i-1 is not working)..

Code i used for displaying next question:
C#
protected void next_Click(object sender, EventArgs e)
    {
        SqlConnection con1 = new SqlConnection(connection);
        con1.Open();
        SqlCommand cmd = new SqlCommand("Select * from quiz1 where id=" + i + " ", con1);
        SqlDataAdapter data = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        data.Fill(ds);
        DataView dv = ds.Tables[0].DefaultView;

        if (dv.Count > 0)
        {
            string id = dv[0]["id"].ToString();
            string question1 = dv[0]["question"].ToString();
            string c1 = dv[0]["c1"].ToString();
            string c2 = dv[0]["c2"].ToString();
            string c3 = dv[0]["c3"].ToString();

        question.Text =question1; // displaying question here
        RadioButton1.Text = c1;   // option 1
        RadioButton2.Text = c2;   // option 2
        RadioButton3.Text = c3;   // option 3

        i = i + 1;
            
        }

}
Posted
Updated 13-Mar-13 4:50am
v4
Comments
[no name] 13-Mar-13 8:40am    
What does "is not working" mean? If you question is about a "previous" button then why post code for a next button that you say works? Why not post the code for the button that does not work?
Ramkumar K 13-Mar-13 8:42am    
only one change in above code... instead of i=i+1. i used i=i-1(for previous button);

1 solution

Firstly I would refactor out your code a bit.

I would create a method called "RetrieveQuestion" that accepts an integer to indicate which question it is getting.

C#
int RetrieveQuestion(int questionNumber)
{
SqlConnection con1 = new SqlConnection(connection);
con1.Open();
SqlCommand cmd = new SqlCommand("Select * from quiz1 where id=" + questionNumber + " ", con1);
SqlDataAdapter data = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
data.Fill(ds);
DataView dv = ds.Tables[0].DefaultView;
 
if (dv.Count > 0)
{
string id = dv[0]["id"].ToString();
string question1 = dv[0]["question"].ToString();
string c1 = dv[0]["c1"].ToString();
string c2 = dv[0]["c2"].ToString();
string c3 = dv[0]["c3"].ToString();
 
question.Text =question1; // displaying question here
RadioButton1.Text = c1; // option 1
RadioButton2.Text = c2; // option 2
RadioButton3.Text = c3; // option 3

return questionNumber +1;
}

return questionNumber; 

}


Then in the Next button event I would have:

C#
protected void next_Click(object sender, EventArgs e)
{
i = RetrieveQuestion(i);
}


and, because RetrieveQuestion returns i+1 in the Previous Button event I would have:

XML
protected void previous_Click(object sender, EventArgs e)
{
i = RetrieveQuestion(i-2);
}


This is because i is currently the next question, not the current question.
 
Share this answer
 
Comments
fjdiewornncalwe 13-Mar-13 9:51am    
There is really no point to refactoring code like this if you don't use paramaterized queries to fix the biggest issue with it. (No vote from me on this one)
Pheonyx 13-Mar-13 9:54am    
The initial refactoring purely saves on duplicating code. You can then look at introducing paramaterized queries after.

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