ADS

Saturday 31 October 2020

Set Question Paper in Online examination system asp .net website C# with SQL server database

 Set Question Paper form Design:

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


<!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></title>

    <style type="text/css">

        .style1

        {

            width: 1024px;

        }

        .style2

        {

            font-family: "Monotype Corsiva";

            font-size: xx-large;

            font-weight: bold;

            color: #3399FF;

            text-align: center;

            padding: 0;

        }

        .style3

        {

            font-size: large;

            font-weight: bold;

            color: #FFFFFF;

            font-family: "Monotype Corsiva";

        }

        .style4

        {

            width: 100%;

        }

        .style5

        {

            text-align: center;

        }

        .style12

        {

            text-align: center;

            width: 165px;

        }

        .style14

        {

            text-align: center;

            width: 166px;

        }

        .style15

        {

            text-align: center;

            width: 165px;

            color: #FFFFFF;

            text-decoration: underline;

        }

        .style16

        {

            width: 228px;

        }

    </style>

</head>

<body bgcolor="#cccccc">

    <form id="form1" runat="server">

    <table align="center" class="style1">

        <tr>

            <td class="style2">

                Online Exam</td>

        </tr>

        <tr>

            <td bgcolor="#3333FF">

                &nbsp;</td>

        </tr>

        <tr>

            <td bgcolor="#99CCFF" 

                style="color: #000099; font-size: medium; font-weight: 700">

                <table align="center" class="style4">

                    <tr>

                        <td class="style12">

                            <a href="adduser.aspx">Add User</a></td>

                        <td class="style12">

                            <a href="deleteuser.aspx">Delete User</a></td>

                        <td class="style15">

                            <a href="setqpaper.aspx">Set Q Paper</a></td>

                        <td class="style14">

                            <a href="paperview.aspx">Preview Q Paper</a></td>

                        <td class="style14">

                            <a href="deleteqpaper.aspx">Delete Q Paper</a></td>

                        <td class="style14">

                            <a href="reports.aspx">View Report</a></td>

                        <td class="style5">

                            <a href="adminlogout.aspx">Logout</a></td>

                    </tr>

                </table>

            </td>

        </tr>

        <tr>

            <td>

                <asp:DropDownList ID="testlist" runat="server" Height="20px" Width="150px" 

                    AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="settype" 

                    DataValueField="settype">

                </asp:DropDownList>

                <asp:TextBox ID="testname" runat="server" Visible="False"></asp:TextBox>

                <asp:Button ID="ntest" runat="server" Font-Bold="True" Text="New Test" 

                    Width="88px" onclick="ntest_Click" />

                <asp:Label ID="msg1" runat="server" ForeColor="Blue"></asp:Label>

                <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

                    ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

                    SelectCommand="SELECT DISTINCT [settype] FROM [question] ORDER BY [settype]">

                </asp:SqlDataSource>

            </td>

        </tr>

        <tr>

            <td>

                <asp:TextBox ID="question" runat="server" Height="50px" Width="600px" 

                    Font-Bold="True"></asp:TextBox>

                <asp:Label ID="msg2" runat="server" ForeColor="Blue"></asp:Label>

            </td>

        </tr>

        <tr>

            <td>

                &nbsp;</td>

        </tr>

        <tr>

            <td>

                <asp:TextBox ID="opt1" runat="server" Width="600px" Font-Bold="True"></asp:TextBox>

                <asp:RadioButton ID="RadioButton1" runat="server" Checked="True" 

                    GroupName="opt" />

                <asp:Label ID="msg3" runat="server" ForeColor="Blue"></asp:Label>

            </td>

        </tr>

        <tr>

            <td>

                &nbsp;</td>

        </tr>

        <tr>

            <td>

                <asp:TextBox ID="opt2" runat="server" Width="600px" Font-Bold="True"></asp:TextBox>

                <asp:RadioButton ID="RadioButton2" runat="server" GroupName="opt" />

                <asp:Label ID="msg4" runat="server" ForeColor="Blue"></asp:Label>

            </td>

        </tr>

        <tr>

            <td>

                &nbsp;</td>

        </tr>

        <tr>

            <td>

                <asp:TextBox ID="opt3" runat="server" Width="600px" Font-Bold="True"></asp:TextBox>

                <asp:RadioButton ID="RadioButton3" runat="server" GroupName="opt" />

                <asp:Label ID="msg5" runat="server" ForeColor="Blue"></asp:Label>

            </td>

        </tr>

        <tr>

            <td>

                &nbsp;</td>

        </tr>

        <tr>

            <td>

                <asp:TextBox ID="opt4" runat="server" Width="600px" Font-Bold="True"></asp:TextBox>

                <asp:RadioButton ID="RadioButton4" runat="server" GroupName="opt" />

                <asp:Label ID="msg6" runat="server" ForeColor="Blue"></asp:Label>

            </td>

        </tr>

        <tr>

            <td>

                <asp:Label ID="msg7" runat="server"></asp:Label>

            </td>

        </tr>

        <tr>

            <td>

                <table align="center" class="style4">

                    <tr>

                        <td class="style16">

                            <asp:Button ID="submit" runat="server" Font-Bold="True" Text="Add Question" 

                                onclick="Button2_Click" />

                        </td>

                        <td>

                            <asp:Button ID="paperview" runat="server" Font-Bold="True" Text="View Paper" />

                        </td>

                    </tr>

                </table>

            </td>

        </tr>

        <tr>

            <td bgcolor="#3333FF">

                &nbsp;</td>

        </tr>

        <tr>

            <td bgcolor="#666666" style="text-align: center">

                <span class="style3">Designed by :- Rohini Kumar Pandey &amp; Suryakant Yadavv</span>

            </td>

        </tr>

    </table>

    </form>

</body>

</html>






----------------------------------------
SetQuestionPaper.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;

public partial class setqpaper : System.Web.UI.Page
{
    HttpCookie ad;
    SqlConnection con;
    SqlCommand cmd;

    SqlCommand rcmd;
    SqlDataReader rdr;

    SqlCommand qcmd;
    SqlDataReader qdr;

    string qry;
    string nqry;
    string test;
    int qid;
    int ans;
    int flag = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        ad = Request.Cookies["acook"];
        if (ad == null)
        {
            Response.Redirect("login.aspx");
        }
        con = new SqlConnection("Data Source=.;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");
        //con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Server.MapPath("~") + "\\App_Data\\Database.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True;");
        msg7.Text = Request.QueryString["msg"];
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        msg1.Text = "";
        msg2.Text = "";
        msg3.Text = "";
        msg4.Text = "";
        msg5.Text = "";
        msg6.Text = "";
        msg7.Text = "";

        if (RadioButton1.Checked)
            ans = 1;
        if (RadioButton2.Checked)
            ans = 2;
        if (RadioButton3.Checked)
            ans = 3;
        if (RadioButton4.Checked)
            ans = 4;

        if (testlist.Visible)
        {
            test = testlist.Text;
        }
        else
        {
            if (testname.Text == "")
            {
                msg1.Text = "Enter the test name";
                flag = 0;
            }
            else
            {
                con.Open();
                nqry = "select * from question where settype='" + testname.Text + "'";
                rcmd = new SqlCommand(nqry, con);
                rdr = rcmd.ExecuteReader();
                if (rdr.HasRows)
                {
                    msg1.Text = "Test name allready exist";
                    flag = 0;
                }
                test = testname.Text;
                con.Close();
            }
        }

        if (question.Text == "" || question.Text == " ")
        {
            msg2.Text = "Enter the Question";
            flag = 0;
        }
        if (opt1.Text == "" || opt1.Text == " ")
        {
            msg3.Text = "Enter the Option 1";
            flag = 0;
        }
        if (opt2.Text == "" || opt2.Text == " ")
        {
            msg4.Text = "Enter the Option 2";
            flag = 0;
        }
        if (opt3.Text == "" || opt3.Text == " ")
        {
            msg5.Text = "Enter the Option 3";
            flag = 0;
        }
        if (opt4.Text == "" || opt4.Text == " ")
        {
            msg6.Text = "Enter the Option 4";
            flag = 0;
        }
        if (flag != 0)
        {
            con.Open();
            qcmd = new SqlCommand("select MAX(qid) from question", con);
            qdr = qcmd.ExecuteReader();
            qdr.Read();

            if (qdr.IsDBNull(0))
            {
                qid = 1;
            }
            else
            {
                qid = Convert.ToInt16(qdr.GetValue(0)) + 1;
            }
            con.Close();

            con.Open();
            qry = "insert into question values(" + qid + ",'" + question.Text + "','" + test + "','" + opt1.Text + "','" + opt2.Text + "','" + opt3.Text + "','" + opt4.Text + "'," + ans + ")";
            cmd = new SqlCommand(qry, con);
            cmd.ExecuteNonQuery();
            con.Close();
            msg7.Text = "Question Submitted";
            Response.Redirect("setqpaper.aspx?msg='"+msg7.Text+"'");
        }
    }

    protected void ntest_Click(object sender, EventArgs e)
    {
        if (testlist.Visible)
        {
            testname.Visible = true;
            testlist.Visible = false;
            ntest.Text = "Old Test";
        }
        else
        {
            testname.Visible = false;
            testlist.Visible = true;
            ntest.Text = "New Test";
        }
    }

}

No comments:

Post a Comment

Popular Posts