Online Test and instant Results

Karthikreddy
Posted by Karthikreddy under ASP.NET category on | Points: 40 | Views : 1527
First create Two Database tables
one for Question and Ans and second one is for Results (Find attached Files)
1. columns in First Table are Quetion,OptionA,OptionB,OptionC,OptionD,CurrectOption
2.columns in second table are UserName,Question ,Ans


<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<p>
<br />
</p>
<p>
Enter UserName:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</p>
<p>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
</p>
<p>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</p>

<asp:RadioButtonList ID="RadioButtonList1" runat="server">
</asp:RadioButtonList>
<p>
<asp:Label ID="Label2" runat="server"></asp:Label>
</p>
<p>
<asp:RadioButtonList ID="RadioButtonList2" runat="server">
</asp:RadioButtonList>
</p>
<p>
<asp:Button ID="Button1" runat="server"
onclick="Button1_Click" Text="Sunmit" />
&nbsp;
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text="Results" />
</p>

<p>
&nbsp;</p>
<p>
</p>
</asp:Content>



CODE BEHIND


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;

public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("Data Source=192.168.1.254;Initial Catalog=jkarthik;User ID=admin;Password=p@ssw0rd");
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack!= true)
{

try
{
con.Open();
SqlCommand cmd = new SqlCommand("select Quetion,OptionA,OptionB,OptionC,OptionD From OnlineTest", con);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Label1.Text = ds.Tables[0].Rows[0]["Quetion"].ToString();
RadioButtonList1.Items.Add(new ListItem(ds.Tables[0].Rows[0]["OptionA"].ToString()));
RadioButtonList1.Items.Add(new ListItem(ds.Tables[0].Rows[0]["OptionB"].ToString()));
RadioButtonList1.Items.Add(new ListItem(ds.Tables[0].Rows[0]["OptionC"].ToString()));
RadioButtonList1.Items.Add(new ListItem(ds.Tables[0].Rows[0]["OptionD"].ToString()));
Label2.Text = ds.Tables[0].Rows[1]["Quetion"].ToString();
RadioButtonList2.Items.Add(new ListItem(ds.Tables[0].Rows[1]["OptionA"].ToString()));
RadioButtonList2.Items.Add(new ListItem(ds.Tables[0].Rows[1]["OptionB"].ToString()));
RadioButtonList2.Items.Add(new ListItem(ds.Tables[0].Rows[1]["OptionC"].ToString()));
RadioButtonList2.Items.Add(new ListItem(ds.Tables[0].Rows[1]["OptionD"].ToString()));

con.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}

}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != "")
{
con.Open();
SqlCommand cmd = new SqlCommand("select Quetion,CurrectOption From OnlineTest", con);
cmd.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
if ((RadioButtonList1.SelectedValue) == ds.Tables[0].Rows[0]["CurrectOption"].ToString())
{
// Response.Write("Correct ");
SqlCommand cmd1 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[0]["Quetion"].ToString() + "','"+1+"')", con);
cmd1.ExecuteNonQuery();

}
else
{

SqlCommand cmd1 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[0]["Quetion"].ToString() + "','" + 0 + "')", con);
cmd1.ExecuteNonQuery();
}
if ((RadioButtonList2.SelectedValue) == ds.Tables[0].Rows[1]["CurrectOption"].ToString())
{
SqlCommand cmd2 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[1]["Quetion"].ToString() + "','" + 1 + "')", con);
cmd2.ExecuteNonQuery();
}
else
{
SqlCommand cmd2 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[1]["Quetion"].ToString() + "','" + 0 + "')", con);
cmd2.ExecuteNonQuery();

}
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Please Enter UserName!')", true);

}
con.Close();

}
protected void Button2_Click(object sender, EventArgs e)
{

if (TextBox1.Text != "")
{
con.Open();
SqlCommand cmd3 = new SqlCommand("select COUNT(*) from Result where UserName='" + TextBox1.Text + "' and Ans='1'", con);
cmd3.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
da.Fill(ds);
int count=Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
if (count > 0)
{
ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Congratulations " + TextBox1.Text + " Your Score is " + ds.Tables[0].Rows[0][0] + " !')", true);
}
else
{
ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Oops ! Sorry " + TextBox1.Text + " Your Score is " + ds.Tables[0].Rows[0][0] + " You are not qualified !')", true);
}
con.Close();
}
else
{
ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Please Enter UserName!')", true);
}
}
}



Thanks

Comments or Responses

Posted by: Tejamanimala on: 3/18/2013 Level:Starter | Status: [Member] | Points: 10
Hi,thank you,i didn't find any attachment,can you please send me attachment files

Login to post response