Wednesday, 13 December 2017


<%@ Page Title="" Language="C#" MasterPageFile="~/MainPage.Master" AutoEventWireup="true" CodeBehind="Exam.aspx.cs" Inherits="OnlineExam.Exam" %>
   
        Student ID : Number of Questions :
       Progrmme : Start Time ::
             Remaining Time ::

Question:  
 
  1.
  2.
  3.
  4.

           
     not visited     not answered     answered      NOT answered & marked review.      answered & marked review.   
           
   First   Previous      |       Last  


<%-- --%>
======================================================================= using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Data; using System.Linq; using System.Collections; using System.Configuration; namespace OnlineExam { public partial class Exam : System.Web.UI.Page { DataSet ds = new DataSet(); SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConOnlineExam"].ConnectionString); SqlDataAdapter adp; SqlCommand cmd , cmd1; int i = 0; int opt; int increment = 0; string s, h, m; private int CurrentPage { get { object objPage = ViewState["_CurrentPage"]; int _CurrentPage = 0; if (objPage == null) { _CurrentPage = 0; } else { _CurrentPage = (int)objPage; } return _CurrentPage; } set { ViewState["_CurrentPage"] = value; } } private int fistIndex { get { int _FirstIndex = 0; if (ViewState["_FirstIndex"] == null) { _FirstIndex = 0; } else { _FirstIndex = Convert.ToInt32(ViewState["_FirstIndex"]); } return _FirstIndex; } set { ViewState["_FirstIndex"] = value; } } private int lastIndex { get { int _LastIndex = 0; if (ViewState["_LastIndex"] == null) { _LastIndex = 0; } else { _LastIndex = Convert.ToInt32(ViewState["_LastIndex"]); } return _LastIndex; } set { ViewState["_LastIndex"] = value; } } PagedDataSource _PageDataSource = new PagedDataSource(); Dal info = new Dal(); public int SIZE = 25; public int mid; public int sid; public String sname; public int ncans; public List questions; public DateTime StartTime; public int curpos = 0; string[] answr = new string[100]; public void BindExam() { try { DataSet BindBatch = (DataSet)Session["questionpaper"]; if (BindBatch.Tables[0].Rows.Count > 0) { DataTable dataTable = BindBatch.Tables[0]; _PageDataSource.DataSource = dataTable.DefaultView; _PageDataSource.AllowPaging = true; _PageDataSource.PageSize = 1; _PageDataSource.CurrentPageIndex = CurrentPage; ViewState["TotalPages"] = _PageDataSource.PageCount; this.lblPageInfo.Text = "Page " + (CurrentPage + 1) + " of " + _PageDataSource.PageCount; this.GridView1.DataSource = _PageDataSource; this.GridView1.DataBind(); this.doPaging(); //this.lbtnPrevious.Enabled = !_PageDataSource.IsFirstPage; //this.lbtnNext.Enabled = !_PageDataSource.IsLastPage; } else { GridView1.DataSource = null; GridView1.DataBind(); } } catch (Exception ex) { } } protected void btnAdd_Click(object sender, EventArgs e) { Response.Redirect("AddProperty.aspx"); } private void doPaging() { try { DataTable dt = new DataTable(); dt.Columns.Add("PageIndex"); dt.Columns.Add("PageText"); fistIndex = CurrentPage - 5; if (CurrentPage > 5) { lastIndex = CurrentPage + 5; } else { lastIndex = 10; } if (lastIndex > Convert.ToInt32(ViewState["TotalPages"])) { lastIndex = Convert.ToInt32(ViewState["TotalPages"]); fistIndex = lastIndex - 10; } if (fistIndex < 0) { fistIndex = 0; } for (int i = fistIndex; i < lastIndex; i++) { DataRow dr = dt.NewRow(); dr[0] = i; dr[1] = i + 1; dt.Rows.Add(dr); } this.dlPaging.DataSource = dt; this.dlPaging.DataBind(); } catch (Exception ex) { throw ex; } } protected void lbtnFirst_Click(object sender, EventArgs e) { CurrentPage = 0; this.BindExam(); } protected void lbtnPrevious_Click(object sender, EventArgs e) { CurrentPage -= 1; this.BindExam(); } protected void lbtnNext_Click(object sender, EventArgs e) { DataSet BindBatch = (DataSet)Session["questionpaper"]; int j = BindBatch.Tables[0].Rows.Count; GridViewRow row = GridView1.Rows[0]; Label hdnField = (Label)row.FindControl("lblQstn"); Label hdnans = (Label)row.FindControl("lblans"); RadioButton r1 = (RadioButton)row.FindControl("ra1"); RadioButton r2 = (RadioButton)row.FindControl("ra2"); RadioButton r3 = (RadioButton)row.FindControl("ra3"); RadioButton r4 = (RadioButton)row.FindControl("ra4"); if (r1.Checked == true || r2.Checked == true || r3.Checked == true || r4.Checked == true) { if (r1 != null && r1.Checked) { opt = 1; } else if (r2 != null && r2.Checked) { opt = 2; } else if (r3 != null && r3.Checked) { opt = 3; } else if (r4 != null && r4.Checked) { opt = 4; } con.Open(); cmd = new SqlCommand("update ExamHistory set Type = 3, Choise = '" + opt + "' where Sid ='" + Convert.ToInt32(Request.QueryString["p"]) + "' and StId = '" + Session["Username"].ToString() + "' and ExamId = '" + Convert.ToInt32(Session["ExamId"]) + "' and Qid = '" + Convert.ToInt32(hdnField.Text) + "' and Ans = '" + hdnans.Text + "' ", con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); } if (j != CurrentPage+1) { CurrentPage += 1; } this.BindExam(); } protected void btnReviewLater_Click(object sender, EventArgs e) { DataSet BindBatch = (DataSet)Session["questionpaper"]; int j = BindBatch.Tables[0].Rows.Count; GridViewRow row = GridView1.Rows[0]; Label hdnField = (Label)row.FindControl("lblQstn"); RadioButton r1 = (RadioButton)row.FindControl("ra1"); RadioButton r2 = (RadioButton)row.FindControl("ra2"); RadioButton r3 = (RadioButton)row.FindControl("ra3"); RadioButton r4 = (RadioButton)row.FindControl("ra4"); if (r1.Checked == true || r2.Checked == true || r3.Checked == true || r4.Checked == true) { if (r1 != null && r1.Checked) { opt = 1; } else if (r2 != null && r2.Checked) { opt = 2; } else if (r3 != null && r3.Checked) { opt = 3; } else if (r4 != null && r4.Checked) { opt = 4; } con.Open(); cmd = new SqlCommand("update ExamHistory set Type = 4, Choise = '" + opt + "' where Sid ='" + Convert.ToInt32(Request.QueryString["p"]) + "' and StId = '" + Session["Username"].ToString() + "' and ExamId = '" + Convert.ToInt32(Session["ExamId"]) + "' and Qid = '" + Convert.ToInt32(hdnField.Text) + "' ", con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); } if (r1.Checked == false && r2.Checked == false && r3.Checked == false && r4.Checked == false) { con.Open(); cmd = new SqlCommand("update ExamHistory set Type = 5 where Sid ='" + Convert.ToInt32(Request.QueryString["p"]) + "' and StId = '" + Session["Username"].ToString() + "' and ExamId = '" + Convert.ToInt32(Session["ExamId"]) + "' and Qid = '" + Convert.ToInt32(hdnField.Text) + "' ", con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); } if (j != CurrentPage + 1) { CurrentPage += 1; } this.BindExam(); } protected void lbtnLast_Click(object sender, EventArgs e) { CurrentPage = (Convert.ToInt32(ViewState["TotalPages"]) - 1); this.BindExam(); } protected void dlPaging_ItemCommand(object source, DataListCommandEventArgs e) { if (e.CommandName.Equals("Paging")) { CurrentPage = Convert.ToInt16(e.CommandArgument.ToString()); this.BindExam(); } } protected void dlPaging_ItemDataBound(object sender, DataListItemEventArgs e) { try { DataSet BindBatch = (DataSet)Session["questionpaper"]; if (e.Item.ItemType != ListItemType.Separator) // if (e.Item.ItemType == ListItemType.Item || //e.Item.ItemType == ListItemType.AlternatingItem) { Button lnkbtnPage = (Button)e.Item.FindControl("lnkbtnPaging"); // if (lnkbtnPage.Text == CurrentPage.ToString()) if (lnkbtnPage.CommandArgument.ToString() == CurrentPage.ToString()) { lblQ.Text = (CurrentPage + 1).ToString(); GridViewRow row = GridView1.Rows[0]; Label hdnField = (Label)row.FindControl("lblQstn"); RadioButton r1 = (RadioButton)row.FindControl("ra1"); RadioButton r2 = (RadioButton)row.FindControl("ra2"); RadioButton r3 = (RadioButton)row.FindControl("ra3"); RadioButton r4 = (RadioButton)row.FindControl("ra4"); if (r1.Checked == false && r2.Checked == false && r3.Checked == false && r4.Checked == false) { adp = new SqlDataAdapter("select Type from ExamHistory where Sid ='" + Convert.ToInt32(Request.QueryString["p"]) + "' and StId = '" + Session["Username"].ToString() + "' and ExamId = '" + Convert.ToInt32(Session["ExamId"]) + "' and Qid = '" + BindBatch.Tables[0].Rows[i]["qid"] + "' ", con); ds = new DataSet(); adp.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { int j = Convert.ToInt32(ds.Tables[0].Rows[0]["Type"]); if (j == 1) { con.Open(); cmd = new SqlCommand("update ExamHistory set Type = 2 where Sid ='" + Convert.ToInt32(Request.QueryString["p"]) + "' and StId = '" + Session["Username"].ToString() + "' and ExamId = '" + Convert.ToInt32(Session["ExamId"]) + "' and Qid = '" + Convert.ToInt32(hdnField.Text) + "' ", con); cmd.CommandType = CommandType.Text; cmd.ExecuteNonQuery(); con.Close(); } } } adp = new SqlDataAdapter("select Type, Choise from ExamHistory where Sid ='" + Convert.ToInt32(Request.QueryString["p"]) + "' and StId = '" + Session["Username"].ToString() + "' and ExamId = '" + Convert.ToInt32(Session["ExamId"]) + "' and Qid = '" + BindBatch.Tables[0].Rows[i]["qid"] + "' ", con); ds = new DataSet(); adp.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { int j = Convert.ToInt32(ds.Tables[0].Rows[0]["Choise"]); if (j == 1) r1.Checked = true; if (j == 2) r2.Checked = true; if (j == 3) r3.Checked = true; if (j == 4) r4.Checked = true; if (j == 0) { r1.Checked = false; r2.Checked = false; r3.Checked = false; r4.Checked = false; } } } adp = new SqlDataAdapter("select Type from ExamHistory where Sid ='" + Convert.ToInt32(Request.QueryString["p"]) + "' and StId = '" + Session["Username"].ToString() + "' and ExamId = '" + Convert.ToInt32(Session["ExamId"]) + "' and Qid = '" + BindBatch.Tables[0].Rows[i]["qid"] + "' ", con); ds = new DataSet(); adp.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { int j = Convert.ToInt32(ds.Tables[0].Rows[0]["Type"]); if (j == 0) { lnkbtnPage.Attributes.Add("class", "btn btn-default"); } if (j == 1) { lnkbtnPage.Attributes.Add("class", "btn btn-default"); } if (j == 2) { lnkbtnPage.Attributes.Add("class", "btn btn-info"); } if (j == 3) { lnkbtnPage.Attributes.Add("class", "btn btn-success"); } if (j == 4) { lnkbtnPage.Attributes.Add("class", "btn btn-warning"); } if (j == 5) { lnkbtnPage.Attributes.Add("class", "btn btn-danger"); } // } } i++; } } catch (Exception ex) { throw ex; } } protected void Page_Load(object sender, EventArgs e) { if (Session["UserName"] == null) { Response.Redirect("index.aspx"); } if (!Page.IsPostBack) { BindExam(); lblSid.Text = Session["UserName"].ToString(); //lblsub.Text = Request.QueryString["subject"]; adp = new SqlDataAdapter("select Name,time from ExamMaster where Id ='" + Convert.ToInt32(Request.QueryString["p"]) + "'", con); ds = new DataSet(); adp.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { lblprogrm.Text = (ds.Tables[0].Rows[0]["Name"]).ToString(); Session["Etime"] = DateTime.Now.AddMinutes(Convert.ToInt32(ds.Tables[0].Rows[0]["time"])); } //lbltopic.Text = Request.QueryString["subtopic"]; //lbllvl.Text = Request.QueryString["level"]; //lblrange.Text = Request.QueryString["range"]; } } protected void timer1_tick(object sender, EventArgs e) { TimeSpan time1 = new TimeSpan(); time1 = (DateTime)Session["Etime"] - DateTime.Now; if (time1.TotalMinutes < 0) { finishtest(); } else { if (time1.Hours.ToString().Length == 1) { h = "0" + time1.Hours.ToString(); } else { h = time1.Hours.ToString(); } if (time1.Minutes.ToString().Length == 1) { m = "0" + time1.Minutes.ToString(); } else { m = time1.Minutes.ToString(); } if (time1.Seconds.ToString().Length == 1) { s = "0" + time1.Seconds.ToString(); } else { s = "0" + time1.Seconds.ToString(); } lblTimer.Text = "hh:mm:ss" + " " + h + ":" + m + ":" + s; } } protected void Button1_Click(object sender, EventArgs e) { finishtest(); } private void finishtest() { DataSet ds2 = new DataSet(); ds2 = (DataSet)Session["questionpaper"]; //Sid is Exam name ID adp = new SqlDataAdapter("select * from ExamHistory where Sid ='" + Convert.ToInt32(Request.QueryString["p"]) + "' and StId = '" + Session["Username"].ToString() + "' and ExamId = '" + Convert.ToInt32(Session["ExamId"]) + "' ", con); ds = new DataSet(); adp.Fill(ds); int u = 0, v = 0; if (ds.Tables[0].Rows.Count > 0) { List cans = new List(); List myansws = new List(); foreach (DataRow row in ds.Tables[0].Rows) { cans.Add(Convert.ToInt32(row["Ans"])); } foreach (DataRow row in ds.Tables[0].Rows) { myansws.Add(Convert.ToInt32(row["Choise"])); } foreach (DataRow row in ds.Tables[0].Rows) { if (Convert.ToInt32(row["Choise"]) == 0) u++; else v++; } int scr; scr = cans.Intersect(myansws).Count(); int m = cans.Count - (scr + u); string prg = lblprogrm.Text; string sub = lblsub.Text; string rng = lblrange.Text; int n = info.insertexamdetails(Session["Username"].ToString(), Convert.ToInt32(Request.QueryString["p"]), Convert.ToInt32(Session["ExamId"]), cans.Count, v, scr, m, u); if (n > 0) { Session["Stoptime"] = DateTime.Now; Response.Redirect("Result.aspx?p=" + n); } if (n == -1) { //lblres.Text = "Exists !!!"; } } } protected void ra1_CheckedChanged(object sender, EventArgs e) { } protected void ra2_CheckedChanged(object sender, EventArgs e) { } protected void ra3_CheckedChanged(object sender, EventArgs e) { } protected void ra4_CheckedChanged(object sender, EventArgs e) { } } }

No comments:

Post a Comment

No.1 Computer Center in India*india's no.1 computer institute ...