Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
sir
have a problem in code for the next question button. i write a code but on clicking the next button it shows only two questions.
my code is as follows:-
C#
public partial class test : System.Web.UI.Page
{
    int x = 0;
    int ans1 = 0;
    SqlConnection ob = new SqlConnection(ConfigurationManager.ConnectionStrings["upsc"].ToString());
     
       
        
    protected void Page_Load(object sender, EventArgs e)
    {
        question();
    }
    protected void question()
    {
        ob.Open();
       
        String a = "Select que,op1,op2,op3,op4,answer from test";
        SqlDataAdapter objcmd = new SqlDataAdapter(a, ob);
        DataSet ds = new DataSet();
        objcmd.Fill(ds, "exam" );
        lblQues.Text = ds.Tables["exam"].Rows[x][0].ToString();
        RadioButton1.Text = ds.Tables["exam"].Rows[x][1].ToString();
        RadioButton2.Text = ds.Tables["exam"].Rows[x][2].ToString();
        RadioButton3.Text = ds.Tables["exam"].Rows[x][3].ToString();
        RadioButton4.Text = ds.Tables["exam"].Rows[x][4].ToString();
        ans.Text = ds.Tables["exam"].Rows[x][5].ToString();
       

        x++;
        ob.Close();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ob.Open();
        
        lblQues.Text = ds.Tables["exam"].Rows[x][0].ToString();
        RadioButton1.Text = ds.Tables["exam"].Rows[x][1].ToString();
        RadioButton2.Text = ds.Tables["exam"].Rows[x][2].ToString();
        RadioButton3.Text = ds.Tables["exam"].Rows[x][3].ToString();
        RadioButton4.Text = ds.Tables["exam"].Rows[x][4].ToString();
        ans.Text = ds.Tables["exam"].Rows[x][5].ToString();


        x++;
    }

   
}
Posted
Updated 5-May-12 16:54pm
v2
Comments
[no name] 5-May-12 23:04pm    
So other than in your button click event, you are not querying the database for the next question and you do not have a WHERE clause to select a question, what is it that your question is?
Shahin Khorshidnia 6-May-12 6:37am    
Reason for my vote of 2
: You didn't Tag your question as ASP.Net or ...

1 solution

As the code shows,

On the Page_Load it loads the first question as x=0 and on the Button1_Click it shows the second question as x becomes 1. Based on the provided code it is doing as you wrote and said.

I assume by saying "it shows only two questions" you might want to display the question in sequence based on the button click. If that's the case, you might try,


  • You might retrieved each question individually based on the current question no which you need to store somewhere, read more about ASP.NET State Management Overview[^]. There might be performance issue for retrieving one question at a time.


Hope it helps :)
 
Share this answer
 
v2
Comments
Shahin Khorshidnia 6-May-12 6:39am    
I think he/she must use hidden field instead of int x
Mohammad A Rahman 6-May-12 6:46am    
I didn't give the OP any solution because there wasn't any clear question, what I did I explain to OP, Op's own code. Come to your point, might be but that is a design issue :)

Thanks anyway.
Shahin Khorshidnia 6-May-12 7:49am    
Ok Mohammad, so you've accepted that your answer was not a solution and it was just a explanation (comment). I think you got the wrong place. (For comment click on "Have a Question or Comment?" and for goving a solution click on "Add a solution")
Mohammad A Rahman 6-May-12 8:11am    
Ok Shahin, so I have accepted that my answer was not a solution and it was just a explanation(comment). Now I have modified it to make it a solution or suggestion which is not a explanation(comment). I think I got the wrong place before as now I modified everything should be sweet :).
Thanks for the guidelines about the Commenting and posting the answer :)
Shahin Khorshidnia 6-May-12 8:16am    
Thank you Mohammad. I know you meant well and I know you can help and give some great solutions.

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