I am making a web application that have student's record and test papers. I want to reload the data from database if computer will be off suddenly(may be due to light issue or laptop battery issue) while student is doing his/her test. All marked answer should be display again when computer turn on again. I can't understand how to do it. Below is my code.
Can anyone tell me what to do.
What I have tried:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.Sql;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
SqlConnection Con;
SqlCommand Cmd;
SqlDataAdapter da;
SqlDataReader dr;
DataSet ds;
DataTable dt = new DataTable();
int recordcount = 0;
int i = 0;
// string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
Con = new SqlConnection("Data Source=DESKTOP-Q69PRF4;Initial Catalog=new;Integrated Security=True");
Cmd = new SqlCommand("SELECT * FROM student where category='"+Session["test"]+ "'", Con);
da = new SqlDataAdapter(Cmd);
ds = new DataSet("student");
Con.Open();
this.da.Fill(ds, "student");
Cmd = new SqlCommand("select * from student where Question='" + TextBox1.Text + "'", Con);
dr = Cmd.ExecuteReader();
while(dr.Read())
{
Label7.Text = dr["Q_ID"].ToString();
lblmarks.Text = dr["TotalMarks"].ToString();
}
recordcount = ds.Tables[0].Rows.Count;
Response.Write("Record Count : " + recordcount);
if (recordcount > i)
{
TextBox1.Text = ds.Tables[0].Rows[i]["Question"].ToString();
Label2.Text = ds.Tables[0].Rows[i]["Choice1"].ToString();
Label3.Text = ds.Tables[0].Rows[i]["Choice2"].ToString();
Label4.Text = ds.Tables[0].Rows[i]["Choice3"].ToString();
Label5.Text = ds.Tables[0].Rows[i]["Choice4"].ToString();
Label6.Text = ds.Tables[0].Rows[i]["Choice5"].ToString();
}
else
{
Response.Write("There are no records in this category");
}
if (i == 0)
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;
}
dr.Read();
Con.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
//Con.Open();
//Cmd = new SqlCommand("select * from student where Question='" + TextBox1.Text + "'", Con);
//dr = Cmd.ExecuteReader();
//while (dr.Read())
//{
// Label7.Text = dr["Q_ID"].ToString();
//}
//Con.Close();
TextBox2.Text = (int.Parse(TextBox2.Text) + 1).ToString();
i = Convert.ToInt32(TextBox2.Text);
TextBox1.Text = ds.Tables[0].Rows[i]["Question"].ToString();
Label2.Text = ds.Tables[0].Rows[i]["Choice1"].ToString();
Label3.Text = ds.Tables[0].Rows[i]["Choice2"].ToString();
Label4.Text = ds.Tables[0].Rows[i]["Choice3"].ToString();
Label5.Text = ds.Tables[0].Rows[i]["Choice4"].ToString();
Label6.Text = ds.Tables[0].Rows[i]["Choice5"].ToString();
if (i == 1)
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
RadioButton5.Visible = false;
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
CheckBox5.Visible = true;
}
else if (i == 2)
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;
}
else if (i == 3)
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
RadioButton5.Visible = false;
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
CheckBox5.Visible = true;
}
else
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;
}
}
protected void previous_Click(object sender, EventArgs e)
{
//Con.Open();
//Cmd = new SqlCommand("select * from student where Question='" + TextBox1.Text + "'", Con);
//dr = Cmd.ExecuteReader();
//while (dr.Read())
//{
// Label7.Text = dr["Q_ID"].ToString();
//}
//Con.Close();
TextBox2.Text = (int.Parse(TextBox2.Text) - 1).ToString();
i = Convert.ToInt32(TextBox2.Text);
// Label7.Text = ds.Tables[0].Rows[i]["Q_ID"].ToString();
TextBox1.Text = ds.Tables[0].Rows[i]["Question"].ToString();
Label2.Text = ds.Tables[0].Rows[i]["Choice1"].ToString();
Label3.Text = ds.Tables[0].Rows[i]["Choice2"].ToString();
Label4.Text = ds.Tables[0].Rows[i]["Choice3"].ToString();
Label5.Text = ds.Tables[0].Rows[i]["Choice4"].ToString();
Label6.Text = ds.Tables[0].Rows[i]["Choice5"].ToString();
if (i == 1)
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
RadioButton5.Visible = false;
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
CheckBox5.Visible = true;
}
else if (i == 2)
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;
}
else if (i == 3)
{
RadioButton1.Visible = false;
RadioButton2.Visible = false;
RadioButton3.Visible = false;
RadioButton4.Visible = false;
RadioButton5.Visible = false;
CheckBox1.Visible = true;
CheckBox2.Visible = true;
CheckBox3.Visible = true;
CheckBox4.Visible = true;
CheckBox5.Visible = true;
}
else
{
RadioButton1.Visible = true;
RadioButton2.Visible = true;
RadioButton3.Visible = true;
RadioButton4.Visible = true;
RadioButton5.Visible = true;
CheckBox1.Visible = false;
CheckBox2.Visible = false;
CheckBox3.Visible = false;
CheckBox4.Visible = false;
CheckBox5.Visible = false;
}
}
protected void Submit_Click(object sender, EventArgs e)
{
string cs = "Data Source=DESKTOP-Q69PRF4;Initial Catalog=new;Integrated Security=True";
SqlConnection con = new SqlConnection(cs);
if (con.State == ConnectionState.Open)
{
con.Close();
}
//con.Open();
//Cmd = new SqlCommand("select * from student where Question='" + TextBox1.Text + "'", con);
//SqlDataReader r = Cmd.ExecuteReader();
////SqlDatareader dr = Cmd.ExecuteReader();
//while (r.Read())
//{
// Label7.Text = r["Q_ID"].ToString();
//}
//con.Close();
con.Open();
if (RadioButton1.Visible == true)
{
if (RadioButton1.Checked)
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblA.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else if (RadioButton2.Checked )
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblB.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else if (RadioButton3.Checked )
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblC.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else if (RadioButton4.Checked )
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblD.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else if (RadioButton5.Checked )
{
string query = "insert into CBE(Q_ID,StID,Answer)values('" + Label7.Text + "','" + Session["new"] + "','" + lblE.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else
{
if (CheckBox1.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans1)values('" + Label7.Text + "','" + Session["new"] + "','" + lblA.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else if (CheckBox2.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans2)values('" + Label7.Text + "','" + Session["new"] + "','" + lblB.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else if (CheckBox3.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans3)values('" + Label7.Text + "','" + Session["new"] + "','" + lblC.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else if (CheckBox4.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans4)values('" + Label7.Text + "','" + Session["new"] + "','" + lblD.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
else if (CheckBox5.Checked)
{
string query = "insert into CBE(Q_ID,StID,Ans5)values('" + Label7.Text + "','" + Session["new"] + "','" + lblE.Text + "')";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
}
}
}
con.Close();
con.Open();
Cmd = new SqlCommand("select Solution from Student where Q_Id='"+Label7.Text+"'",con);
SqlDataReader dr = Cmd.ExecuteReader();
while(dr.Read())
{
Label8.Text = dr["Solution"].ToString();
}
con.Close();
//con.Open();
//string qr = "Update CBE Set Answer='" + Label8.Text + "'where Q_ID='"+Label7.Text+"'";
//Cmd = new SqlCommand(qr,con);
//dr = Cmd.ExecuteReader();
//while (dr.Read())
//{
// lblans.Text = dr["Answer"].ToString();
//}
//con.Close();
con.Open();
Cmd = new SqlCommand("select * from CBE where Q_ID='" + Label7.Text + "'", con);
dr = Cmd.ExecuteReader();
while (dr.Read())
{
if (RadioButton1.Visible == true)
{
if (RadioButton1.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (RadioButton2.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (RadioButton3.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (RadioButton4.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (RadioButton5.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else
{
lblcheck.Text = "";
}
}
else
{
if (CheckBox1.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (CheckBox2.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (CheckBox3.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (CheckBox4.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else if (CheckBox5.Checked)
{
lblcheck.Text = dr["Answer"].ToString();
}
else
{
lblcheck.Text = "";
}
}
}
con.Close();
con.Open();
Cmd = new SqlCommand("Select TotalMarks from Student where Q_ID='" + Label7.Text + "'", con);
dr = Cmd.ExecuteReader();
while(dr.Read())
{
lblques.Text = dr["TotalMarks"].ToString();
}
con.Close();
con.Open();
if (lblcheck.Text == Label8.Text)
{
Cmd = new SqlCommand("Update CBE Set StMarks='" + lblques.Text + "'where Q_ID='" + Label7.Text + "'", con);
}
else if (lblcheck.Text != Label8.Text)
{
Cmd = new SqlCommand("Update CBE Set StMarks='" + lblm.Text + "'where Q_ID='" + Label7.Text + "'", con);
}
else
lblques.Text = "Please Select at least one option";
Cmd.ExecuteNonQuery();
con.Close();
}
void clear()
{
RadioButton1.Checked = false;
RadioButton2.Checked = false;
RadioButton3.Checked = false;
RadioButton4.Checked = false;
RadioButton5.Checked = false;
CheckBox1.Checked = false;
CheckBox2.Checked = false;
CheckBox3.Checked = false;
CheckBox4.Checked = false;
CheckBox5.Checked = false;
}
}