TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
priya dharshini
NA
11
4.6k
error in coding by calculating the results of online exam
Feb 26 2015 12:33 AM
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;
using System.Data;
using System.Collections;
using System.Timers;
using System.Web.SessionState;
using System.IO;
namespace student_management.studpro
{
public partial class WebForm3 : System.Web.UI.Page
{
int TotQus=0, ResultId = 100;
static int count = 0;
static int viewradio = 0;
float sepmarks = 0.0f;
static float marks = 0.0f, correct = 0.0f;
int totalSeconds = 0, seconds = 60, minutes = 60, selectedcount = 0, originalcount = 0, crt = 0, wr = 0;
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["aloproConnectionString"].ConnectionString);
string selected=null,choose;
string subject_name, Result = null, UserName = null;
static String CrtAns = null, ChoiceType = null;
//string constr = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\Administrator\\Documents\\alopro.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
DataTable dt;
DataSet ds;
SqlDataAdapter ad;
static int rowindex = -1;
protected void Page_Load(object sender, EventArgs e)
{
subject_name = Request.QueryString["Subject_name"];
String oldSelectedSubject = subject_name;
UserName =(string)Session["UserName"];
Session["Subject_name"] = oldSelectedSubject;
string noofqns = "SELECT [Questions] FROM [testsettings]";
SqlCommand cmd = new SqlCommand(noofqns, con);
con.Open();
TotQus = Convert.ToInt16(cmd.ExecuteScalar().ToString());
ds = new DataSet();
ad = new SqlDataAdapter("SELECT * FROM addsubject", con);
ad.Fill(ds,"addsubject");
dt = ds.Tables["addsubject"];
con.Close();
if (viewradio == 0)
{
RadioInvisible();
}
else
{
RadioVisible();
}
viewradio += 1;
CheckBoxInvisible();
}
protected void Timer1_tick(object sender, EventArgs e)
{
Session["time"] = Convert.ToInt16(Session["time"]) - 1;
if (Convert.ToInt16(Session["time"]) <= 0)
{
ButtonNext.Visible = false;
RadioInvisible();
ScriptManager.RegisterStartupScript(Page,typeof(Page),"OpenWindow","window.open('/studpro/uresults.aspx'?Marks="+marks+"');",true);
string strscript="<script language=javascript>window.opener='';window.top.close();</script>";
Page.RegisterStartupScript("clientScript",strscript);
}
else
{
totalSeconds = Convert.ToInt16(Session["time"]);
seconds = totalSeconds % 60;
minutes = totalSeconds / 60;
clock.Text = minutes + ":" + seconds;
}
}
public string SelectedAnswer()
{
if (RadioButton1.Checked)
{
return "A";
}
else if (RadioButton2.Checked)
{
return "B";
}
else if (RadioButton3.Checked)
{
return "C";
}
else if (RadioButton4.Checked)
{
return "D";
}
else
{
return "nothing";
}
}
protected string ChooseAnswer()
{
if (CheckBox1.Checked)
{
choose = "A,";
}
if (CheckBox2.Checked)
{
choose += "B,";
}
if (CheckBox3.Checked)
{
choose += "C,";
}
if (CheckBox4.Checked)
{
choose += "D,";
}
return choose;
}
protected void RadioUncheck()
{
RadioButton1.Checked = false;
RadioButton2.Checked = false;
RadioButton3.Checked = false;
RadioButton4.Checked = false;
}
protected void CheckBoxUncheck()
{
CheckBox1.Checked = false;
CheckBox2.Checked = false;
CheckBox3.Checked = false;
CheckBox4.Checked = false;
}
protected void RadioInvisible()
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
}
protected void RadioVisible()
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
}
protected void CheckBoxVisible()
{
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
}
protected void CheckBoxInvisible()
{
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
}
protected void ButtonStart_Click(object sender, EventArgs e)
{
Response.Write("<script language='javascript'>alert('TimeStarts now');</script>");
string query = "SELECT [Time] FROM [testsettings]";
SqlCommand cmd = new SqlCommand(query, con);
con.Open();
int timeget = Convert.ToInt16(cmd.ExecuteScalar().ToString());
totalSeconds = timeget * 60;
Timer1.Enabled = true;
Session["time"] = totalSeconds;
rowindex = rowindex + 1;
count = count + 1;
try
{
if (rowindex < TotQus)
{
ChoiceType = dt.Rows[rowindex]["ChooseAnswer"].ToString();
lblnoofquestions.Text = count.ToString() + " of " + TotQus;
lblQuestion.Text = dt.Rows[rowindex]["Questions"].ToString();
Option1.Text = dt.Rows[rowindex]["Answera"].ToString();
Option2.Text = dt.Rows[rowindex]["Answerb"].ToString();
Option3.Text = dt.Rows[rowindex]["Answerc"].ToString();
Option4.Text = dt.Rows[rowindex]["Answerd"].ToString();
CrtAns = dt.Rows[rowindex]["Correctans"].ToString();
if(ChoiceType =="Single")
{
RadioVisible();
CheckBoxInvisible();
}
else
{
RadioInvisible();
CheckBoxVisible();
Report();
}
}
else
{
ButtonNext.Visible = false;
RadioInvisible();
CheckBoxInvisible();
marks = (float)(((float)(correct) / (float)(TotQus)) * 100);
if (marks >= 50)
{
Result = "pass";
}
else
{
Result = "fail";
}
Report();
ScriptManager.RegisterStartupScript(Page, typeof(Page), "Openwindow", "window.open('studpro/uresults.aspx'?Marks=" + marks + "');", true);
string strscript = "<script language=javascript>window.opener='';window.top.close();</script>";
Page.RegisterStartupScript("clientScript", strscript);
}
}
catch (IndexOutOfRangeException)
{
Response.Write("Questions Over");
}
finally
{
ButtonStart.Visible = false;
ButtonNext.Visible = true;
con.Close();
}
}
protected void Report()
{
try
{
string InsQuery = "INSERT into results VALUES('" + ResultId.ToString() + "','" + UserName + "','" + subject_name + "','" + DateTime.Today + "','" + TotQus + "','" + marks + "','" + Result + "')";
//con.Open();
SqlCommand cmd2 = new SqlCommand(InsQuery, con);
cmd2.ExecuteNonQuery();
}
catch (Exception ex)
{
Response.Write(ex);
Response.Write("tryagain");
}
finally
{
//con.Close();
}
}
protected void ButtonNext_Click(object sender, EventArgs e)
{
con.Open();
count += 1;
try
{
if (rowindex < TotQus)
{
rowindex = rowindex + 1;
ChoiceType = dt.Rows[rowindex]["ChooseAnswer"].ToString();
lblnoofquestions.Text = count.ToString() + " of " + TotQus;
lblQuestion.Text = dt.Rows[rowindex]["Questions"].ToString();
Option1.Text = dt.Rows[rowindex]["Answera"].ToString();
Option2.Text = dt.Rows[rowindex]["Answerb"].ToString();
Option3.Text = dt.Rows[rowindex]["Answerc"].ToString();
Option4.Text = dt.Rows[rowindex]["Answerd"].ToString();
CrtAns = dt.Rows[rowindex]["Correctans"].ToString();
}
}
catch (Exception)
{
Response.Write("<script language='javascript'>alert('Wait utill time over');</script>");
}
finally
{
if (ChoiceType == "Single")
{
selected = SelectedAnswer();
if (CrtAns == selected)
{
correct = correct + 1;
}
}
else
{
selected = ChooseAnswer();
string selectedn = selected.Remove(selected.Length - 1, 1);
ArrayList userSelected = new ArrayList();
ArrayList originalvalues = new ArrayList();
string[] originalAns = CrtAns.Split(',');
string[] myStr=selectedn.Split(',');
foreach (string original in originalAns)
{
originalvalues.Add(original);
}
foreach(string original in myStr )
{
userSelected.Add(original);
}
originalcount = originalvalues.Count;
float orgnct = (float)originalcount;
sepmarks = (1.0f) / orgnct;
correct = correct - 1;
if (selectedcount > originalcount)
{
crt = 0;
Response.Write(" Crt :" + crt);
}
else
{
Response.Write("empty");
for (int i = 0; i < selectedcount; i++)
{
if (originalvalues.Contains(userSelected[i]))
{
crt += 1;
}
else
{
wr += 1;
}
}
}
correct += (sepmarks * crt);
Response.Write(correct);
}
}
RadioUncheck();
CheckBoxUncheck();
con.Close();
}
}
Reply
Answers (
1
)
Connection details to connect to an SQL2014 Instance in EF 6
crystal report error