Hi to all,
I am developing an online examination system using ASP.NET with C#. This is my test.aspx.cs page. This project is for online test 1 Question and 4 ans.
There are next,previous buttons to choose questions. The problem is that the previous button is not working. Please suggest me what to do?
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.Text;
public partial class StartTest : System.Web.UI.Page
{
public static SqlConnection sqlconn;
protected string PostBackStr;
SqlDataReader dr;
int test_no,len,k;
DataRow dtr;
int[] arr = new int[6];
long timerStartValue = 1800;
protected void Page_Load(object sender, EventArgs e)
{
sqlconn = new SqlConnection(ConfigurationManager.AppSettings["ConnectionString"].ToString());
int[] a = (int[])Session["values"];
if (!IsPostBack)
{
this.timerStartValue = long.Parse(ConfigurationManager.AppSettings["Delay"].ToString());
this.TimerInterval = 500;
Panel1.Visible = true;
loadquestion();
int i = Convert.ToInt32(Session["Counter"]);
if (i == 1)
{
btnprevious.Enabled = false;
}
}
}
public void loadquestion()
{
if (Convert.ToInt32(Session["Counter"]) <= 6)
{
test_no = Convert.ToInt32(Session["Test"]);
Session["seltestno"] = test_no;
int[] a = (int[])Session["values"];
string str = "select * from Quest_Info,Test_Final where Test_Final.Q_id=Quest_Info.Quest_id and Test_Final.Q_id=" + a[Convert.ToInt32(Session["Counter"]) - 1] + " and Quest_Info.Test_id='1' and Test_Final.Serial='1' order by Test_Final.Q_id";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
DataSet ds2 = new DataSet();
da2.Fill(ds2, "Result");
if (ds2.Tables[0].Rows.Count > 0)
{
dtr = ds2.Tables[0].Rows[0];
string question = dtr["Question"].ToString();
string ch1 = dtr["choice1"].ToString();
string ch2 = dtr["choice2"].ToString();
string ch3 = dtr["choice3"].ToString();
string ch4 = dtr["choice4"].ToString();
string cans = dtr["correct"].ToString();
Session["Answer"] = cans;
Session["qid"] = dtr["Q_id"].ToString();
display(question, ch1, ch2, ch3, ch4);
}
}
else
{
lblName.Visible = false;
lblQuestion.Visible = false;
RblOption.Visible = false;
btnnext.Visible = false;
btnprevious.Visible = false;
LinkButton1.Visible = true;
btnsubmit.Visible = false;
{
Session["rblsel"] = RblOption.SelectedIndex + 1;
int rb = RblOption.SelectedIndex + 1;
if (Session["Answer"].ToString() == rb.ToString())
{
int score = Convert.ToInt32(txtScore.Text) + 1;
txtScore.Text = score.ToString();
lblScore.Text = "Score : " + Convert.ToString(score);
}
int i = Convert.ToInt32(Session["Counter"]);
Session["Counter"] = ++i;
SqlCommand cmd = new SqlCommand("INSERT INTO Sel_Ans VALUES ('1', @Quid, @Selected, '@marks')", sqlconn);
SqlParameter Quid = cmd.Parameters.Add("@Quid", SqlDbType.Int, 50);
Quid.Value = Convert.ToInt32(Session["Counter"]);
SqlParameter Selected = cmd.Parameters.Add("@Selected", SqlDbType.Int, 50);
Selected.Value = Session["rblsel"];
sqlconn.Open();
cmd.ExecuteNonQuery();
sqlconn.Close();
loadquestion();
}
else
{
RegisterStartupScript("myAlert", "<script>alert('Please select the option')</script>");
}
}
public void storeans()
{
}
void Page_PreRender(object sender, EventArgs e)
{
StringBuilder bldr = new StringBuilder();
bldr.AppendFormat("var Timer = new myTimer({0},{1},'{2}','timerData');", this.timerStartValue, this.TimerInterval, this.lblTimerCount.ClientID);
bldr.Append("Timer.go()");
ClientScript.RegisterStartupScript(this.GetType(), "TimerScript", bldr.ToString(), true);
ClientScript.RegisterHiddenField("timerData", timerStartValue.ToString());
}
void Page_PreInit(object sender, EventArgs e)
{
string timerVal = Request.Form["timerData"];
if (timerVal != null || timerVal == "")
{
timerVal = timerVal.Replace(",", String.Empty);
timerStartValue = long.Parse(timerVal);
}
}
private Int32 TimerInterval
{
get
{
object o = ViewState["timerInterval"];
if (o != null) { return Int32.Parse(o.ToString()); }
return 50;
}
set { ViewState["timerInterval"] = value; }
}
protected void btnnext_Click(object sender, EventArgs e)
{
nextquestion();
selectans();
int i = Convert.ToInt32(Session["Counter"]);
int f = Convert.ToInt32(Session["rblsel"].ToString());
if (i > 1)
{
btnprevious.Enabled = true;
}
}
protected void btnprevious_Click(object sender, EventArgs e)
{
selectans();
int f = Convert.ToInt32(Session["rblsel"].ToString());
int i = Convert.ToInt32(Session["Counter"]);
Session["Counter"] = --i;
if (i > 0)
{
loadquestion();
int[] b = (int[])Session["values"];
foreach (int p in b)
{
RblOption.SelectedIndex = f - 1;
}
}
}
#region Connection Open
public void ConnectionOpen()
{
try
{
if (sqlconn.State == ConnectionState.Closed) { sqlconn.Open(); }
}
catch (SqlException ex)
{
}
catch (SystemException sex)
{
}
}
#endregion
#region Connection Close
public void ConnectionClose()
{
try
{
if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); }
}
catch (SqlException ex)
{
}
catch (SystemException sex)
{
}
}
#endregion
protected void LinkButton1_Click(object sender, EventArgs e)
{
Session["Counter"] = 1;
lblName.Visible = true;
lblQuestion.Visible = true;
RblOption.Visible = true;
btnnext.Visible = true;
btnprevious.Visible = false;
LinkButton1.Visible = false;
btnsubmit.Visible = false;
int[] b = (int[])Session["values"];
int f = Convert.ToInt32(Session["rblsel"].ToString());
int[] t = (int[])Session["arrofans"];
LinkButton1.Enabled = false;
LinkButton1.Text = "You have finished test with" + " " + Session["score"].ToString();
lblTimerCount.Visible = false;
loadquestion();
selectans();
}
public void selectans()
{
int[] a = (int[])Session["values"];
string str = "select * from Sel_Ans where Quid=" + a[Convert.ToInt32(Session["Counter"]) - 1] + " and Tid='1'";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
DataSet ds2 = new DataSet();
da2.Fill(ds2, "Result");
if (ds2.Tables[0].Rows.Count > 0)
{
dtr = ds2.Tables[0].Rows[0];
string selected = dtr["Selected"].ToString();
Session["sel"] = selected;
Response.Write(selected);
RblOption.SelectedIndex = Convert.ToInt32(selected);
}
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
Response.Redirect("Result.aspx");
}
}