Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have written the code but it is not working properly
when i click on next button it is showing me next record program works properly but whan I log out and log in directly shows me last record not from start..please reply
Question table colums are question_id,teacher_id,teacher_question,teacher_answer

//code is


namespace project_master
{
    public partial class acnj : System.Web.UI.Page
    {
              
        SqlCommand cmd = new SqlCommand();
        public static int i = 0;
          DataTable dt = new DataTable();
        DataRow drr;
        SqlConnection con = new SqlConnection(@"Data Source=.\sqlexpress;Initial Catalog=swatidb;Integrated Security=True;Pooling=False");
         public void Page_Load(object sender, EventArgs e)
        {
            con.Open();
            string s1 = "select student_id from Student where user_name='" + Session["sid"] + "'";
            SqlCommand cmd = new SqlCommand(s1, con);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Label4_stu_id.Text =dr[0].ToString();//for displaying student_id
                dr.Close();
            }
            SqlDataAdapter da = new SqlDataAdapter("select * from Question where question_id>=1 and question_id<=3", con);
            da.Fill(dt);
            drr = dt.Rows[i];
 
            Label2_tea_que.Text = "Write question below in Textbox  click next to get questoion";
           // i++;
        }
         public void next_Click(object sender, System.EventArgs e)
         {
             if (i == (dt.Rows.Count - 1))
             {
                 Response.Write("Last record !");
             }
             else
             {
                 i++;
             }

             Label3_que_id.Text = Convert.ToString(drr[0]);//this label for displaying question_id
             Label2_tea_que.Text = Convert.ToString(drr[2]); //this label for displaying Teacher question
          }
       }   
    }
Posted
Updated 22-Mar-14 7:47am
v5
Comments
Richard MacCutchan 20-Mar-14 6:11am    
it is not working properly

And what exactly does that mean?
swati gapat 22-Mar-14 7:42am    
sir now i have updated question please reply...
Sergey Alexandrovich Kryukov 20-Mar-14 10:30am    
"Code for button" is already written. What is your problem?
—SA
swati gapat 22-Mar-14 7:44am    
when i click on next button it is showing me next record, program works properly but whan I log out and log in directly shows me last record not from start give me reply i also tried with declaring i variable as non-static
ZurdoDev 20-Mar-14 11:01am    
When it loads, load the first question. What's the issue?

1 solution

Basically, there are two approaches to this problem, you can store the current question (currently the i variable as a session variable, e.g.
C#
Session["CurrentQuestion"]
, and when the user logs out, make sure to clean and abandon the session like this:
C#
Session.Clear();
Session.Abandon();

Otherwise, you can use the IsPostback property to reset the current question:
C#
public void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        i = 0;
    }

    ...
}

This will only reset the current question on the first visit to the page, not after navigating to the next or previous question
 
Share this answer
 
Comments
swati gapat 23-Mar-14 6:03am    
thanks sir... for giving me this solution..I was triyng solve from two days

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