E-Quiz And Online Examination Result Module | Display The Score Obtained

The very first problem faced while working on E-Quiz project is how to show the total marks scored by the user. Or to be very precise the question comes HOW TO DISPLAY RESULT…??  in simple terms.

Well I too faced such a situation, and with the help of my colleague have found the solution to this problem.

This code is valid only for ASP.Net Applications but the logic can be implemented while working in other technologies.

In this We’ve used GridView control to overcome this thing. The code below will help you understand better and will explain few things which are necessary.

First of all you have to create a table in the database which will contain Id, Question, Options(no. depends upon the developer) and Answer as fields.

Below is the screen shot of the table.

Paras Bannar

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

As one can see in the above the table, the key point to remember is, always store option no. in the answer field and not the complete answer as this kind of approach increases the optimization and feasibility to use code and more over it reduces the coding effort.


The steps you’ll follow while coding the module….

Firstly, code the structure of Grid View (i.e customize grid view according to your requirement) see this link How To Customize GridView.

Second Step, Execute a query which fetches random records from database, see this link How To Fetch Random Values From Data Base, and bind it up with the GridView.

Third Step, this is the main step in which you will fetch random values…… (explain later in the post) for time being  just go through the code.

Default.aspx

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView runat="server" id="grd" AutoGenerateColumns="false">
        <Columns>
        <asp:TemplateField HeaderText="Question">
        <ItemTemplate>
           <asp:Label ID="qstn" runat="server" Text='<%#Eval("question") %>'></asp:Label><br />
            <asp:RadioButton ID="rad1" runat="server" Text='<%#Eval("opt1") %>' GroupName="A" />
            <asp:RadioButton ID="rad2" runat="server" Text='<%#Eval("opt2") %>' GroupName="A" />
            <asp:RadioButton ID="rad3" runat="server" Text='<%#Eval("opt3") %>' GroupName="A" />
            <asp:RadioButton ID="rad4" runat="server" Text='<%#Eval("opt4") %>' GroupName="A" />
            <asp:HiddenField ID="hf" runat="server" Value='<%#Eval("answer") %>' />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>
 
        <asp:Button ID="btn" runat="server" Text="Submit" OnClick="btn_Click" />
    </div>
    </form>
</body>
</html>

 

Default.aspx.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Default10 : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand cmd;
    SqlDataReader dr;
    string str = ConfigurationManager.ConnectionStrings["myconnectionstring"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack == false)
        {
            showrec();
           
        }
    }

    private void showrec()
    {
        con = new SqlConnection();
        con.ConnectionString = str;

        cmd = new SqlCommand();
        cmd.CommandText = "select TOP 5 * FROM qstn ORDER BY NEWID()";

        cmd.Connection = con;
        con.Open();

        dr = cmd.ExecuteReader();
        if (dr.HasRows)
        {
            grd.DataSource = dr;
            grd.DataBind();
        }
        else
        {
            Response.Write("No daTa");
        }
        con.Close();
    }
  
    protected void btn_Click(object sender, EventArgs e)
    {
        RadioButton r1, r2, r3, r4;
        HiddenField hdn;
        int count = 0;
        string selans = "-1";
        for (int i = 0; i < grd.Rows.Count; i++)
        {
            r1 = (RadioButton)grd.Rows[i].Cells[0].FindControl("rad1");
            r2 = (RadioButton)grd.Rows[i].Cells[0].FindControl("rad2");
            r3 = (RadioButton)grd.Rows[i].Cells[0].FindControl("rad3");
            r4 = (RadioButton)grd.Rows[i].Cells[0].FindControl("rad4");
            hdn = (HiddenField)grd.Rows[i].Cells[0].FindControl("hf");
            if (r1.Checked)
            {
                selans = "1";
            }
            else if (r2.Checked)
            {
                selans = "2";
            }
            else if (r3.Checked)
            {
                selans = "3";
            }
            else if (r4.Checked)
            {
                selans = "4";
            }

            if (hdn.Value == selans)
            {
                count++;
            }
            btn.Text = "Score = " + count;
        }
        

    }
}

 

Code Explanation

In Default.aspx page, I’ve taken Label Control in order to display question form the database,

4 radio buttons in order to display four option related to a particular question, and last but not the least, I’ve used hidden field in which I will store answer of the particular question.

And finally I’ve taken a Button Control, for which I’ve created the onclick event, on which I will perform the operation to generate score.

Now lets come to Default.aspx.cs,

here on page load even and function showrec() is called in which I wrote code to fetch random question from database (this you might have seen in my earlier post(s)).

Now the main thing we have to focus on is the onclick event of the button, as you can see, I’ve fetch the controls form the aspx page using code mentioned above, and further I’ve used if statement in order to see which radio button is active and have store the corresponding value in the varialbe “selans”, using this “selans”, I will compare it with the value of  hidden field in order to find whether checked radio button is the correct answer or not, it the answer is correct, i.e value in “selans” matches with the value in hidden field ( the actual answer) and the variable “count” ( initially initialized with value 0) increments accordingly, and all this code is placed in the “for loop” which will execute till the no. of controls in the GridView (you can relate it with the no. of question, as for every record GridView generates new control).

Hope this post was helpful. Download link is available below.

Download Demo Work


JavaScript, ASP.Net & PHP Web Developer. Connect with me on Facebook and Twitter.

Share This Post

Related Articles

Powered by Paras Babbar · Designed by Paras Babbar