Vidya Sawant

Vidya Sawant

  • NA
  • 1
  • 719

Test Result

Mar 26 2016 10:14 AM
hii sir , I need a code for storing a test result in databse. below i m giving my test code. plzzgive me code, I need to create result and that result must store in database and also student can view him/her result and also admin. Plzz give me code plzz plzzz plzzz
test.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.Data;
using System.Configuration;
using System.Timers;

public partial class LTest : System.Web.UI.Page
{

SqlConnection conn = new SqlConnection("Data Source = VIDYA\\SQLEXPRESS; Initial Catalog = EExamData; Integrated Security = True");
SqlCommand comm = new SqlCommand();
static DataSet ds = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{


if (!Page.IsPostBack)
{


comm.Connection = conn;
comm.CommandText = "select distinct(sid) from Questions";
conn.Open();
SqlDataReader dr = comm.ExecuteReader();
comm.CommandTimeout = 0;
DropDownList2.DataSource = dr;
DropDownList2.DataTextField = "sid";
DropDownList2.DataBind();
DropDownList2.Items.Insert(0, new ListItem("--Select--", "0"));
conn.Close();
btnTryAgain.Visible = false;
btnBackToHomePage.Visible = false;


}
}
static int index = 0;
static int answer = 0;
static int wrong = 0;
static int skip = 0;
protected void btnSubmit_Click(object sender, EventArgs e)
{
RadioButton1.Enabled = true;
RadioButton2.Enabled = true;
RadioButton3.Enabled = true;
RadioButton4.Enabled = true;
// Button2.Enabled = true;
if (btnSubmit.Text == "Next")
{
string user_ans = "";
if (RadioButton1.Checked == true)
{
user_ans = "1";
}
else if (RadioButton2.Checked == true)
{
user_ans = "2";
}
else if (RadioButton3.Checked == true)
{
user_ans = "3";
}
else if (RadioButton4.Checked == true)
{
user_ans = "4";
}
string cor_ans = ds.Tables[0].Rows[index]["cans"].ToString();
if (user_ans == cor_ans)
{
answer++;
}
else
{
wrong++;
}
index++;

Label1.Text = ds.Tables[0].Rows[index]["question"].ToString();
RadioButton1.Text = ds.Tables[0].Rows[index]["ans1"].ToString();
RadioButton2.Text = ds.Tables[0].Rows[index]["ans2"].ToString();
RadioButton3.Text = ds.Tables[0].Rows[index]["ans3"].ToString();
RadioButton4.Text = ds.Tables[0].Rows[index]["ans4"].ToString();
RadioButton1.Checked = false;
RadioButton2.Checked = false;
RadioButton3.Checked = false;
RadioButton4.Checked = false;
btnSkip.Enabled = true;

if (ds.Tables[0].Rows.Count == index + 1)
{
btnSubmit.Text = "Submit";
//Button2.Visible = false;
}

}
else
{
string user_ans = "";
if (RadioButton1.Checked == true)
{
user_ans = "1";
}
else if (RadioButton2.Checked == true)
{
user_ans = "2";
}
else if (RadioButton3.Checked == true)
{
user_ans = "3";
}
else if (RadioButton4.Checked == true)
{
user_ans = "4";
}
string cor_ans = ds.Tables[0].Rows[index]["cans"].ToString();
if (user_ans == cor_ans)
{
answer++;
}
else
{
wrong++;
}
Label2.ForeColor = System.Drawing.Color.Green;
Label3.ForeColor = System.Drawing.Color.Red;
Label4.ForeColor = System.Drawing.Color.Blue;
Label2.Text = "Correct Ans = " + answer.ToString();
Label3.Text = "Incorrect Ans = " + wrong.ToString();
Label4.Text = "Skipped Question = " + skip.ToString();

btnTryAgain.Visible = true;
btnBackToHomePage.Visible = true;
btnSubmit.Enabled = false;


index = 0;
answer = 0;
skip = 0;
wrong = 0;
}

}
protected void show(object sender, EventArgs e)
{
RadioButton1.Enabled = false;
RadioButton2.Enabled = false;
RadioButton3.Enabled = false;
RadioButton4.Enabled = false;
btnSkip.Enabled = false;

}
//skip
protected void btnSkip_Click(object sender, EventArgs e)
{




RadioButton1.Enabled = true;
RadioButton2.Enabled = true;
RadioButton3.Enabled = true;
RadioButton4.Enabled = true;

if (btnSubmit.Text == "Next")
{
string user_ans = "";
if (RadioButton1.Checked == true)
{
user_ans = "1";
}
else if (RadioButton2.Checked == true)
{
user_ans = "2";
}
else if (RadioButton3.Checked == true)
{
user_ans = "3";
}
else if (RadioButton4.Checked == true)
{
user_ans = "4";
}
string cor_ans = ds.Tables[0].Rows[index]["cans"].ToString();
if (user_ans == cor_ans)
{
answer++;
}
else
{
wrong++;
}
index++;
skip++;
Label1.Text = ds.Tables[0].Rows[index]["question"].ToString();
RadioButton1.Text = ds.Tables[0].Rows[index]["ans1"].ToString();
RadioButton2.Text = ds.Tables[0].Rows[index]["ans2"].ToString();
RadioButton3.Text = ds.Tables[0].Rows[index]["ans3"].ToString();
RadioButton4.Text = ds.Tables[0].Rows[index]["ans4"].ToString();
RadioButton1.Checked = false;
RadioButton2.Checked = false;
RadioButton3.Checked = false;
RadioButton4.Checked = false;
btnSkip.Enabled = true;

if (ds.Tables[0].Rows.Count == index + 1)
{
btnSubmit.Text = "Submit";

}

}

else if (ds.Tables[0].Rows.Count == index + 1)
{

skip++;
string user_ans = "";
if (RadioButton1.Checked == true)
{
user_ans = "1";
}
else if (RadioButton2.Checked == true)
{
user_ans = "2";
}
else if (RadioButton3.Checked == true)
{
user_ans = "3";
}
else if (RadioButton4.Checked == true)
{
user_ans = "4";
}
string cor_ans = ds.Tables[0].Rows[index]["cans"].ToString();
if (user_ans == cor_ans)
{
answer++;
}
else
{
wrong++;
}
Label2.ForeColor = System.Drawing.Color.Green;
Label3.ForeColor = System.Drawing.Color.Red;
Label4.ForeColor = System.Drawing.Color.Blue;
Label6.ForeColor = System.Drawing.Color.Black;
Label6.Text = "Quiz Result :";
Label2.Text = "Correct Ans = " + answer.ToString();
Label3.Text = "Incorrect Ans = " + wrong.ToString();
Label4.Text = "Skipped Question = " + skip.ToString();

btnTryAgain.Visible = true;
btnBackToHomePage.Visible = true;
btnSubmit.Enabled = false;
btnSkip.Enabled = false;
RadioButton1.Enabled = false;
RadioButton2.Enabled = false;
RadioButton3.Enabled = false;
RadioButton4.Enabled = false;
index = 0;
answer = 0;
skip = 0;
wrong = 0;

}

}

protected void btnGo_Click1(object sender, EventArgs e)
{
if (DropDownList2.SelectedValue == "1")
{

Label5.Text = "Physics";
ds.Clear();
index = 0;
answer = 0;
SqlDataAdapter da = new SqlDataAdapter("Select Top 60 Question,ans1,ans2,ans3,ans4,cans from Questions where sid=1 ORDER BY NEWID()", conn);
da.Fill(ds);
Label1.Text = ds.Tables[0].Rows[0]["question"].ToString();
RadioButton1.Text = ds.Tables[0].Rows[0]["ans1"].ToString();
RadioButton2.Text = ds.Tables[0].Rows[0]["ans2"].ToString();
RadioButton3.Text = ds.Tables[0].Rows[0]["ans3"].ToString();
RadioButton4.Text = ds.Tables[0].Rows[0]["ans4"].ToString();
MultiView1.ActiveViewIndex = 0;
Panel1.Visible = false;
}
else if (DropDownList2.SelectedValue == "2")
{

Label5.Text = "Chemistry";
ds.Clear();
index = 0;
answer = 0;
SqlDataAdapter da = new SqlDataAdapter("Select Top 60 Question,ans1,ans2,ans3,ans4,cans from Questions where sid=2 ORDER BY NEWID()", conn);
da.Fill(ds);
Label1.Text = ds.Tables[0].Rows[0]["question"].ToString();
RadioButton1.Text = ds.Tables[0].Rows[0]["ans1"].ToString();
RadioButton2.Text = ds.Tables[0].Rows[0]["ans2"].ToString();
RadioButton3.Text = ds.Tables[0].Rows[0]["ans3"].ToString();
RadioButton4.Text = ds.Tables[0].Rows[0]["ans4"].ToString();
MultiView1.ActiveViewIndex = 0;
Panel1.Visible = false;
}
else if (DropDownList2.SelectedValue == "3")
{

Label5.Text = "Mathematics";
ds.Clear();
index = 0;
answer = 0;
SqlDataAdapter da = new SqlDataAdapter("Select Top 60 Question,ans1,ans2,ans3,ans4,cans from Questions where sid=3 ORDER BY NEWID()", conn);
da.Fill(ds);
Label1.Text = ds.Tables[0].Rows[0]["question"].ToString();
RadioButton1.Text = ds.Tables[0].Rows[0]["ans1"].ToString();
RadioButton2.Text = ds.Tables[0].Rows[0]["ans2"].ToString();
RadioButton3.Text = ds.Tables[0].Rows[0]["ans3"].ToString();
RadioButton4.Text = ds.Tables[0].Rows[0]["ans4"].ToString();
MultiView1.ActiveViewIndex = 0;
Panel1.Visible = false;
}

}
protected void btnBackToHomePage_Click(object sender, EventArgs e)
{
Response.Redirect("Testinfo.aspx");
}
protected void btnTryAgain_Click(object sender, EventArgs e)
{
Response.Redirect("LTest.aspx");
}




//.............................................................../////////////////









protected void logout4_Click(object sender, EventArgs e)
{
Session.RemoveAll();
Session["New"] = null;
Response.Redirect("login.aspx");
}




protected void Button1_Click(object sender, EventArgs e)
{
Response.Redirect("testinfo.aspx");
}
}


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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.auto-style1 {
height: 100px;
width: 207px;
}
.auto-style2 {
height: 100px;
width: 858px;
}
</style>

<table class="auto-style8">
<tr>
<td class="auto-style11"><img src="Images/njkm,.png" class="auto-style1" /></td>
<td class="auto-style11"><img src="Images/get 123.jpg" class="auto-style2"/></td>
</tr>
</table>
</head>
<body>
<form id="form1" runat="server">
<div>
<div>
<link href="Styles/StyleSheetMain.css" rel="stylesheet" />
</div>

<div>
<br />
<asp:Button ID="Button1" runat="server" Text="Back" Font-Bold="True" Height="29px" OnClick="Button1_Click" PostBackUrl="~/testinfo.aspx" Width="84px" />
<asp:button runat="server" text="Logout" Font-Bold="True" Height="29px" Width="85px" ID="logout4" OnClick="logout4_Click" PostBackUrl="~/login.aspx"/>
</div>
<div style="background-color:violet; text-align: right; width: 478px;">
<asp:Panel ID="Panel1" runat="server" Height="61px">
<table style="width: 76%; float: left; height: 71px">
<tr>
<td style="text-align: right; width: 93px">Select Quiz</td>
<td style="width: 179px; text-align: center;">
<asp:DropDownList ID="DropDownList2" runat="server" Height="31px" Width="136px">
</asp:DropDownList><br />
</td>
<td style="text-align: left">
<asp:Button ID="btnGo" runat="server" OnClick="btnGo_Click1" style="margin-left: 0px" Text="Go" Height="30px" Width="102px" BackColor="#FFFFCC" Font-Bold="True" ForeColor="#FF0066" BorderColor="Violet" />
</td>
</tr>
</table>

</asp:Panel>
</div>



<br />
<br />
<table style="width: 99%; float: left; height: 301px;">
<tr>
<td style="width: 20px">&nbsp;</td>
<asp:MultiView ID="MultiView1" runat="server">

<asp:View ID="View2" runat="server">
<td style="width: 954px">


<div style="color:black">


<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>


<asp:Label ID="Label7" runat="server" Font-Bold="True" style="text-decoration: underline" Font-Names="Sylfaen" Font-Size="X-Large"></asp:Label><br />
<asp:Label ID="Label5" runat="server" Font-Bold="True" style="text-decoration: underline" Font-Names="Monotype Corsiva" Font-Size="XX-Large" ForeColor="#3333CC"></asp:Label><br /><br />
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Size="XX-Large" ForeColor="#009900"></asp:Label>
<br /><br /> <hr />
<br />
<asp:RadioButton ID="RadioButton1" runat="server" AutoPostBack="True" GroupName="a" OnCheckedChanged="show" Font-Size="Large" ForeColor="#CC0066" /><br />
<asp:RadioButton ID="RadioButton2" runat="server" AutoPostBack="True" GroupName="a" OnCheckedChanged="show" Font-Size="Large" ForeColor="#CC0066" />
<br />
<asp:RadioButton ID="RadioButton3" runat="server" AutoPostBack="True" GroupName="a" OnCheckedChanged="show" Font-Size="Large" ForeColor="#CC0066" /><br />
<asp:RadioButton ID="RadioButton4" runat="server" AutoPostBack="True" GroupName="a" OnCheckedChanged="show" Font-Size="Large" ForeColor="#CC0066" /></div>
<br /><br />


<table style="width: 169px"><tr><td>
<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Next" Height="36px" Width="81px" /></td>
<td> <asp:Button ID="btnSkip" runat="server" OnClick="btnSkip_Click" Text="Skip" Height="36px" Width="81px" /></td>
</tr>
</table><br /><hr />
<asp:Label ID="Label6" runat="server" BorderStyle="None" style="font-weight: 700" Font-Bold="True" Font-Size="XX-Large" ForeColor="#0033CC"></asp:Label>
<br />
<br />

<asp:Label ID="Label2" runat="server" BorderStyle="None" Font-Bold="False" Font-Size="X-Large"></asp:Label>
<br />
<br />
<asp:Label ID="Label4" runat="server" BorderStyle="None" Font-Bold="False" Font-Size="X-Large"></asp:Label>
<br /><br />
<asp:Label ID="Label3" runat="server" BorderStyle="None" Font-Bold="False" Font-Size="X-Large"></asp:Label>

<br />
<br />

<br />


<asp:LinkButton ID="btnTryAgain" runat="server" OnClick="btnTryAgain_Click" Font-Bold="True" Font-Italic="True" Font-Names="Monotype Corsiva" Font-Size="X-Large" Height="30px" Width="197px">Back To Test Page</asp:LinkButton>
&nbsp;

&nbsp;&nbsp;
<asp:LinkButton ID="btnBackToHomePage" runat="server" OnClick="btnBackToHomePage_Click" Font-Bold="True" Font-Italic="True" Font-Names="Monotype Corsiva" Font-Size="X-Large" Height="30px" Width="121px">Try Again</asp:LinkButton>
<br />
<br />
<br />
<br />
<br />
<br />
</asp:View>

</asp:MultiView>
<td>&nbsp;</td>
</tr>
</table>

<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br />
<br /> </div>
</form>
</body>
</html>
plzz give me reply