-1

I'am implementing a quiz portal type web application.

First question is fetching fine from db. clicking on next button,next question is fetching fine. Now again clicking on next or prev button nothing happens!

Concept used - made a global variable which will be equal to the current question no. from my questions table, incrementing/decrementing variable on next/prev clicks

why this approach is not working! what code should i change in Page_Load or else where?

note:

1.disconnected model used

2.countdown timer is working fine on postbacks,no issue.

Attached images..

1.question table design here

2.On default page(ques1) and on clicking next button(ques2) Q1. and Q2. combined image

public partial class quiz : System.Web.UI.Page
{
int qno=0;  //global variable
DataTable dt;

protected void Page_Load(object sender, EventArgs e)
{

    if (Session["sname"] == null)
    {
        Response.Redirect("home.aspx");
    }
    string qpname = Request.QueryString["qpname"]; //question paper table name

    //**some countdown timer code here**

    SqlConnection conn = null;
    conn = new SqlConnection();
    conn.ConnectionString = "Data Source=xyz; Initial Catalog=xzyyzz;Integrated Security=True;";

     String queryString = "select * from " + qpname + "";
     SqlCommand cmd = new SqlCommand(queryString, conn);      
     SqlDataAdapter ad = new SqlDataAdapter(queryString, conn);
     dt = new DataTable();
     ad.Fill(dt);


     foreach (DataRow row in dt.Rows)
      {
          Label6.Text = row["marks"].ToString(); //for marks of current ques. 
          Label7.Text = row["ques"].ToString();  //for ques.
          qno = (int)row["qno"]; //ques no.
          qno = qno + 1; //
          break;
       }

}


protected void Timer1_Tick(object sender, EventArgs e)
{
    //some timer code here
}

public class CountDownTimer
{
   //some timer code here
}

protected void submit_Click(object sender, EventArgs e)
{

    Response.Redirect("student.aspx"); //redirect on submit

}

protected void prev_Click(object sender, EventArgs e)   //prev button
{

    display_ques(-1);

}
protected void next_Click(object sender, EventArgs e)   //next button
{
    display_ques(1);
}

public void display_ques(int direction)
{
    DataRow[] result = dt.Select(" qno = " +qno+ ""); //for current ques no.

    foreach (DataRow row in result)
    {
        Label6.Text = row["marks"].ToString();
        Label7.Text = row["ques"].ToString();
        if (direction == 1)
            qno=qno+1;
        else
            qno=qno-1;
        break;

    }
}
}

please help guys! stuck on this..

Jai hind
  • 85
  • 1
  • 11

1 Answers1

1

If you want to persist some value between postbacks use Session or ViewState.

Session["qno"] = (int)row["qno"];

How to take the the value from Session.

int qno = 0;
if(Session["qno"] != null)
   qno = int.TryParse(Session["qno"].ToString(), out qno);
mybirthname
  • 17,949
  • 3
  • 31
  • 55