Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am currently doing a project on online examination.

I store the correct answer in a session object. When i move on to the next webpage the value of the session object changes. How can I solve this problem?

Code:

C#
Ans += sdr["corAns"].ToString();
Session["Ans"] = Ans;

// value is 1412
2nd web page:
C#
string Rans = Session["Ans"].ToString(); // value is1214

Please help me. I'm new in programming.
Posted
Updated 15-Mar-12 22:39pm
v2
Comments
OriginalGriff 16-Mar-12 4:48am    
How are you checking the value when you set the value, and how are you checking it in the new page?
rose rao 16-Mar-12 5:06am    
i used a label to check it

Hi rose,

Debug your Code By Breakpoints To Check ,Is there any Code that is Changing The Value Of Session....?
 
Share this answer
 
I tried, but i can't find a solution. This is my code.
C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;


using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


public partial class xfianlexam : System.Web.UI.Page
{
    SqlCommand cmd1;
    public string qnum;
    public string Ans = null;
    public int tNUM;
    protected void Page_Load(object sender, EventArgs e)
    {
        lblStuNum.Text = Session["id"].ToString();
        SqlConnection cn = myClass.DBCon();
        cn.Open();
        SqlCommand c = new SqlCommand("select * from student where snum='" + Session["id"].ToString() + "'", cn);

        SqlDataReader r = c.ExecuteReader();

        r.Read();
        lblStuName.Text = r["sname"].ToString();
        Session["sname"] = lblStuName.Text;

        cn.Close();
        
        
        SqlConnection scn = myClass.DBCon();
        scn.Open();
        SqlCommand cmd2 = new SqlCommand("select * from course where courseid='" + Session["courseid"].ToString() + "'", scn);
        SqlDataReader ssr = cmd2.ExecuteReader();

        ssr.Read();
        lblCourseid.Text = ssr["courseid"].ToString();
        Session["courseid"] = lblCourseid.Text;


        scn.Close();



        Session["name"] = lblStuName.Text;
    

        int i = 1;
       SqlConnection con = myClass.DBCon();
        
        con.Open();
      
          const int Total = 10;
            ArrayList lstNumbers = xfianlexam.RandomNumbers(Total);

            for (int i1= 0; i1 < lstNumbers.Count; i1++)
            {

               
                cmd1 = new SqlCommand("select * from Question where Courseid='" + Session["courseid"].ToString() + "'and questOrder='" + lstNumbers[i1] + "'", con);
               
                SqlDataReader sdr = cmd1.ExecuteReader();
                while (sdr.Read())
                {
                    
                    Literal littxt = new Literal();
                    Literal litti = new Literal();

                    RadioButtonList cbk = new RadioButtonList();
                    cbk.ID = "cbk" + i.ToString();
        littxt.Text = "<font style="background : #FFF8C6;">" +i.ToString() + "、" + Server.HtmlEncode(sdr["Quest"].ToString())+"</font><br><blockquote>";
                    litti.Text = "</blockquote>";
                   

                    qnum = i.ToString();
                    Session["qnum"] = qnum;
                    cbk.Items.Add("A. " + Server.HtmlEncode(sdr["opt1"].ToString()));
                    cbk.Items.Add("B. " + Server.HtmlEncode(sdr["opt2"].ToString()));
                    cbk.Items.Add("C. " + Server.HtmlEncode(sdr["opt3"].ToString()));
                    cbk.Items.Add("D. " + Server.HtmlEncode(sdr["opt4"].ToString()));
                    cbk.Font.Size = 11;
                    for (int j = 1; j <= 4; j++)
                    {
                        cbk.Items[j - 1].Value = j.ToString();
                    }


                    Label7.Text += sdr["corAns"].ToString();
                    Ans += sdr["corAns"].ToString();


                    Label5.Text = Ans.ToString();
                
                         Session["Ans"] = Ans;
                  
                    Panel2.Controls.Add(littxt);
                    Panel2.Controls.Add(cbk);
                    Panel2.Controls.Add(litti);

                    i++;
                    tNUM++;

                    
                }

                sdr.Close();
                //sdr10.Close();
                cn.Close();
            }
        Label8.Text = Label5.Text;
        

      


        }

    public static ArrayList RandomNumbers(int max)
    {
        // Create an ArrayList object that will hold the numbers
        ArrayList lstNumbers = new ArrayList();
        // The Random class will be used to generate numbers
        Random rndNumber = new Random();

        // Generate a random number between 1 and the Max
        int number = rndNumber.Next(1, max + 1);
        // Add this first random number to the list
        lstNumbers.Add(number);
        // Set a count of numbers to 0 to start
        int count = 0;

        do // Repeatedly...
        {
            // ... generate a random number between 1 and the Max
            number = rndNumber.Next(1, max + 1);

            // If the newly generated number in not yet in the list...
            if (!lstNumbers.Contains(number))
            {
                // ... add it
                lstNumbers.Add(number);
            }

            // Increase the count
            count++;
        } while (count <= 10 * max); // Do that again

        // Once the list is built, return it
        return lstNumbers;
    }
    protected void Finish_Click(object sender, EventArgs e)
    {


    }
  
        protected void ibtnFin_Click(object sender, EventArgs e)
    {
     
        string msc = "";
        for (int i = 1; i <= tNUM; i++)
        {
            RadioButtonList list = (RadioButtonList)Panel2.FindControl("cbk" + i.ToString());
            if (list != null)
            {
                if (list.SelectedValue.ToString() != "")
                {

                    msc += list.SelectedValue.ToString();
                }
                else
                {
                    msc += "0";
                }
            }
        }
        Session["Sans"] = msc;
        //----------------------------
        Label9.Text = msc;
        Session["snum"] = lblStuNum.Text;
              SqlConnection cn = myClass.DBCon();
        cn.Open();
        string sno = lblStuNum.Text;
            SqlConnection cn2 = myClass.DBCon();

        cn2.Open();
        SqlCommand cmd = new SqlCommand("select count(*) from result where sname='" + Session["sname"].ToString() + "' and courseid='" + Session["courseid"].ToString() + "'", cn2);
        int i1 = Convert.ToInt32(cmd.ExecuteScalar());

        if (i1 > 0)
        {
            Response.Write("<script language='javascript'>alert('You have already participated in this examination');</script>");
            Response.Redirect("studHome.aspx");
        }
        else
        {
            SqlDataSource sds1 = new SqlDataSource();

            sds1.ConnectionString = ConfigurationManager.ConnectionStrings["examConnectionString"].ToString();

            lblStuNum.Text = Session["id"].ToString();
            sds1.InsertCommand = "insert into result(sname,courseid) values (@sname,@courseid)";

            sds1.InsertParameters.Add("sname", Session["sname"].ToString());
            sds1.InsertParameters.Add("courseid", Session["courseid"].ToString());
          





            int rowsAffect = sds1.Insert();

       
            Response.Redirect("stuResult.aspx?BInt=" + tNUM.ToString());

        }
        
    }


        protected void btnsubmit_Click(object sender, EventArgs e)
        {

        }
}
 
Share this answer
 
v3
Comments
Bojjaiah 16-Mar-12 5:18am    
Edit and add pre tag
seems code inside Page_Load runs on every roundtrip and it may be the issue.Is it done in any purpose . If not you can Check ISPostBack and can restrict the code to run once in Page Load

Its just a wild guess
 
Share this answer
 
v2
I got it! Thank you soo much!

I have 1 more problem now, the RadioButtonList is taking a null value. Why is that so?

RadioButtonList list = (RadioButtonList)Panel2.FindControl("cbk" + i.ToString());
 
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