Create Online Exam Project with ASP.Net & C#.Net with JavaScript Timer


This Code Snippet demonstrates a sample Online Exam application using ASP.Net and C#.Net, SQL Server 2005. First it takes Name and then set a timer to the questions with this name. It uses JavaScript for displaying Timer. Use this application to create simple online exam application.

Description:



This Code Snippet demonstrates a sample Online Exam. It takes Name first then set a timer to the question. It uses the timer in JavaScript.

Technical Features:



1. Uses ASP.Net and HTML controls like TextBox, Button, span
2. Simple JavaScript code to handle timer
3. Fetch Questions from SQL Server Database and Fetch Random Questions
4. It also Demostrates the JAVAScript POSTBACK function in a simple way.
5. It automatically PostBack when time is over

Code



count.js

var hour = 0; //specify hours for counter
var min = 0; // specify minutes
var second = 59; // specify the seconds
var lab = 'cd'; // id of the entry on the page where the counter is to be inserted

function start() {
displayCountdown(setCountdown(hour, min, second), lab);
}
loaded(lab, start);
var pageLoaded = 0;
window.onload = function() { pageLoaded = 1; }
function loaded(i, f) {
if (document.getElementById && document.getElementById(i) != null)
f();
else if (!pageLoaded)
setTimeout('loaded(\'' + i + '\',' + f + ')', 100);
}
function setCountdown(hour, min, second) {
if (hour > 0)
min = min * hour * 60;
c = setC(min, second);
return c;
}
function setC(min, second) {
if (min > 0)
second = min * 60 * second;
return Math.floor(second);
}
function displayCountdown(countdn, cd) {
if (countdn < 0) {
alert("Sorry, you are too late.");
__doPostBack('__Page', 'time');
}
else {
var secs = countdn % 60;
if (secs < 10)
secs = '0' + secs;
var countdn1 = (countdn - secs) / 60;
var mins = countdn1 % 60;
if (mins < 10)
mins = '0' + mins;
countdn1 = (countdn1 - mins) / 60;
var hours = countdn1 % 24;
document.getElementById(cd).innerHTML = hours + ' : ' + mins + ' : ' + secs;
setTimeout('displayCountdown(' + (countdn - 1) + ',\'' + cd + '\');', 999);
}
}



Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Online Exam Page</title>
<script type="text/javascript" src="count.js">
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Font-Size="Medium" ForeColor="DarkRed" Style="z-index: 100;
left: 64px; position: absolute; top: 19px" Text="Name : " Width="68px"></asp:Label>

<asp:TextBox ID="txtName" runat="server" Style="z-index: 101; left: 146px; position: absolute;
top: 18px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Style="z-index: 102; left: 321px; position: absolute;
top: 18px" Text="Start Exam" ToolTip="Enter Your Name" OnClick="Button1_Click" />
<asp:TextBox ID="txtScore" runat="server" Style="z-index: 103; left: 681px; position: absolute;
top: 276px" Visible="False" Width="63px">0</asp:TextBox>

<asp:Panel ID="Panel1" runat="server" BackColor="#E0E0E0" BorderColor="#E0E0E0" Height="264px"
Style="z-index: 104; left: 60px; position: absolute; top: 54px" Visible="False"
Width="707px" ForeColor="#0000C0">
<asp:Label ID="lblName" runat="server" Style="z-index: 100; left: 13px; position: absolute;
top: 10px" Text="Name : " ForeColor="#0000C0" Width="387px"></asp:Label>

<asp:Label ID="lblScore" runat="server" ForeColor="Green" Style="z-index: 102; left: 567px;
position: absolute; top: 11px" Text="Score : " Width="136px"></asp:Label>
<asp:Panel ID="Panel3" runat="server" Height="14px" Width="119px" style="left:427px; z-index: 106; position: absolute; top: 10px;">
<span id="cd" style ="left:100px;"></span>
</asp:Panel>
<asp:Panel ID="Panel2" runat="server" Height="214px" Style="z-index: 103; left: 8px;
position: absolute; top: 41px" Width="696px">
<asp:Label ID="lblQuestion" runat="server" Style="z-index: 100; left: 3px; position: absolute;
top: 7px" Text="Label" Width="682px"></asp:Label>
<asp:RadioButtonList ID="RblOption" runat="server" Style="z-index: 102; left: 30px;
position: absolute; top: 36px" Width="515px">
</asp:RadioButtonList>
<asp:Button ID="Button2" runat="server" Style="z-index: 106; left: 289px; position: absolute;
top: 178px" Text="Next" ToolTip="Click Here to Take Next Question" OnClick="Button2_Click" />
<asp:Button ID="Finish" runat="server" Style="z-index: 106; left: 289px; position: absolute;
top: 178px" Text="Finish" ToolTip="Click Here to Finish The Test" Visible ="false" OnClick="Finish_Click" />

</asp:Panel>
<asp:Label ID="lblResult" Style="z-index: 107; left: 189px; position: absolute;
top: 128px" runat="server" Visible ="false" Font-Bold ="true" ForeColor ="Green" Text=""></asp:Label>
</asp:Panel>


</div>
</form>
</body>
</html>



Default.aspx.cs


public partial class _Default : System.Web.UI.Page
{
public static SqlConnection sqlconn;
protected string PostBackStr;
int startid = 1;//Here specify your starting id of Questions table. So that it will display questions from id starting from this value
int endid = 4;//Here specify your ending id of Questions table. So that it will display questions which has id below this value
int totalnoofquestions = 4;//Here change the number of questions you want to display.
protected void Page_Load(object sender, EventArgs e)
{
sqlconn = new SqlConnection("Enter Connection String");
PostBackStr = Page.ClientScript.GetPostBackEventReference(this, "time");

if (IsPostBack)
{
string eventArg = Request["__EVENTARGUMENT"];
if (eventArg == "time")
{
getNextQuestion();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Visible = false;
txtName.Visible = false;
Button1.Visible = false;
Panel1.Visible = true;
lblName.Text = "Name : " + txtName.Text;
int score = Convert.ToInt32(txtScore.Text);
lblScore.Text = "Score : " + Convert.ToString(score);
Session["counter"] = "1";
Random rnd = new Random();
int i = rnd.Next(startid ,endid+1 );
getQuestion(i);
ArrayList al = new ArrayList();
al.Add(i.ToString());
Session["ids"] = al;
}
protected void Button2_Click(object sender, EventArgs e)
{
getNextQuestion();
}
protected void Finish_Click(object sender, EventArgs e)
{
if (Session["Answer"].ToString() == RblOption.SelectedIndex.ToString())
{
int score = Convert.ToInt32(txtScore.Text) + 1;// 1 for mark for each question
txtScore.Text = score.ToString();
lblScore.Text = "Score : " + Convert.ToString(score);
}
lblResult.Text = "Thank you for test our application. Your Score is : " + txtScore.Text;
lblResult.Visible = true;
Panel2.Visible = false;
}
public void getQuestion(int no)
{

string str = "select * from Questions where id=" + no + "";
SqlDataAdapter da2 = new SqlDataAdapter(str, sqlconn);
DataSet ds2 = new DataSet();
da2.Fill(ds2, "Question");
if (ds2.Tables[0].Rows.Count > 0)
{
DataRow dtr;
int i = 0;
while (i < ds2.Tables[0].Rows.Count)
{
dtr = ds2.Tables[0].Rows[i];
Session["Answer"] = Convert.ToString(Convert.ToInt32(dtr["Correct"].ToString()) - 1);
lblQuestion.Text = "Q." + Session["counter"].ToString() + " " + dtr["Question"].ToString();
RblOption.ClearSelection();
RblOption.Items.Clear();
RblOption.Items.Add(dtr["Option1"].ToString());
RblOption.Items.Add(dtr["Option2"].ToString());
RblOption.Items.Add(dtr["Option3"].ToString());
RblOption.Items.Add(dtr["Option4"].ToString());
i++;
}
}
}
public void getNextQuestion()
{
//Finish.Visible = false;
if (Convert.ToInt32(Session["counter"].ToString()) < totalnoofquestions)
{
if (RblOption.SelectedIndex >= 0)
{
if (Session["Answer"].ToString() == RblOption.SelectedIndex.ToString())
{
int score = Convert.ToInt32(txtScore.Text) + 1;// 1 for mark for each question
txtScore.Text = score.ToString();
lblScore.Text = "Score : " + Convert.ToString(score);
}
}
Random rnd = new Random();
int i = rnd.Next(startid ,endid );
ArrayList al = (ArrayList)Session["ids"];
if (!al.Contains(i.ToString()))
{
al.Add(i.ToString());
}
else
{
while (al.Contains(i.ToString()))
{
i = rnd.Next(startid, endid + 1);

if (al.Count == totalnoofquestions - 1 && !al.Contains(i.ToString()))
{
Button2.Visible = false;
Finish.Visible = true;
break;
}
else if (al.Count > endid + 1)
{
break;
}

}
if (!al.Contains(i.ToString()))
{
al.Add(i.ToString());
}
}
if (al.Count == totalnoofquestions)
{
Button2.Visible = false;
Finish.Visible = true;
}
Session["ids"] = al;
Session["counter"] = Convert.ToString(Convert.ToInt32(Session["counter"].ToString()) + 1);
getQuestion(i);



}
else
{
Panel2.Visible = false;
//code for displaying after completting the exam, if you want to show the result then you can code here.
}
}
public void ConnectionOpen()
{
try
{
if (sqlconn.State == ConnectionState.Closed) { sqlconn.Open(); }
}
catch (SqlException ex)
{ }
catch (SystemException sex)
{ }
}
public void ConnectionClose()
{
try
{
if (sqlconn.State != ConnectionState.Closed) { sqlconn.Close(); }
}
catch (SqlException ex)
{ }
catch (SystemException sex)
{ }
}
}



For complete source code download from below links :



1. http://www.filesonic.com/file/1056137774/Online_Exam.rar

2. http://uploading.com/files/61b49c7m/Online%2BExam.rar/

3. http://www.fileserve.com/file/xzHKsDm

4. http://letitbit.net/download/89171.8bf5accc88dd342ad76a4e04f313/Online_Exam.rar.html


Thank you.

Reference: http://dotnetsquare.com/resources/43-create-online-exam-project-with-Asp-Net-C-Sharp-net-with-javascript-timer


Comments

Author: siraj15 May 2012 Member Level: Bronze   Points : 0

the above links are dead .can u post once again on these forum
thanks

Author: Manoranjan Sahoo26 Jun 2012 Member Level: Gold   Points : 0

The below link is working properly.

http://uploading.com/files/61b49c7m/Online%2BExam.rar/

Guest Author: arvind18 Feb 2013

hi could you please explan what table structure you have used for this project and could you please explain pageload() part in the above code. thank you in advance

Guest Author: chethana30 May 2013

Table structure for the above
USE [dbNacons]
GO
/****** Object: Table [dbo].[Question] Script Date: 05/30/2013 15:34:36 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Question](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Question] [varchar](max) NULL,
[Correct] [int] NULL,
[Option1] [varchar](max) NULL,
[Option2] [varchar](max) NULL,
[Option3] [varchar](max) NULL,
[Option4] [varchar](max) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF

Guest Author: shagun sinha29 Jul 2013

Rest is perfect only getting an error where "Array List" is mentioned.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: